您当前的位置: 首页 >  spring

梁云亮

暂无认证

  • 1浏览

    0关注

    1211博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SpringBoot+SpringSecurity系列一:环境搭建

梁云亮 发布时间:2021-05-20 17:05:23 ,浏览量:1

第一步:创建项目
  • 添加依赖:

    org.springframework.boot
    spring-boot-starter-web



    org.springframework.boot
    spring-boot-starter-security



    org.springframework.boot
    spring-boot-starter-test
    test


    org.springframework.security
    spring-security-test
    test



    org.springframework.boot
    spring-boot-starter-thymeleaf

  • 修改application.yml:
server:
  port: 80
  servlet:
    context-path: /ssd

spring:
  thymeleaf:
    #前缀,也就是模板存放的路径
    prefix: classpath:/templates/
    #编码格式
    encoding: UTF-8
    check-template-location: false
    #关闭缓存,不然无法看到实时页面
    cache: false
    #后缀
    suffix: .html
    #设置不严格的html
    mode: HTML
    servlet:
      content-type: text/html
第二步:在resources/templates目录下创建文件secret.html



  
  受保护的资源


受保护的资源


第三步:创建做请求分发的Controller:
@Controller
public class DispatchController {
    @GetMapping("/secret")
    String secret() {
        return "secret";
    }
}
默认用户名和密码
  • 启动日志,生成的默认用户的密码: 在这里插入图片描述
  • 访问http://localhost:80/ssd/secret,直接打开SpringSecurity提供的登录页面,网址为http://localhost/ssd/login:如下图所示: 在这里插入图片描述
  • 在其中输入错误的用户名和密码,结果: 在这里插入图片描述
  • 在其中输入正确的用户名user和密码,会直接打开secret.html页面: 在这里插入图片描述 在这里插入图片描述
配置文件中指定用户名和密码
  • 1.修改application.yml,添加如下内容:
spring:
  security:
    user:
      name: zhangsan
      password: 1234
  • 2.再次启动项目,发现没有生成随机的密码值
  • 3.启动项目测试,只有输入zhangsan-1234时才能登录成功 在这里插入图片描述
退出登录

网址:http://localhost/ssd/logout 在这里插入图片描述 单击Log Out后: 在这里插入图片描述 以后再访问资源时,就又需要登录了

关注
打赏
1665409997
查看更多评论
立即登录/注册

微信扫码登录

0.0422s