文档 Spring Security Reference SpringBoot+SpringSecurity+jwt整合及初体验 JSON Web Token 入门教程 - 阮一峰 JWT 官网
SpringSecurity 项目 GitHub 仓库地址:https://github.com/aaronlinv/springsecurity-jwt-demo
依赖 主要用到了: SpringSecurity,Thymeleaf,Web,Lombok
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependency> 页面 编写页面和 Controller 进行测试,具体页面可以看 代码 主要包含了首页(index),订单(order),还有 user,role,menu这三个位于 /system 下,需要 admin 权限
使用内存用户进行表单登录 在 static 下新建 login.html,用于登录
<form action="/login" method="post"> <label for="username">账户</label><input type="text" name="username" id="username"><br> <label for="password">密码</label><input type="password" name="password" id="password"><br> <input type="submit" value="登录"> </form> 编写继承 WebSecurityConfigurerAdapter 的 Security 配置类,并开启 @EnableWebSecurity 注解,这个注解包含了 @Configuration WebSecurityConfigurerAdapter 中有两个方法,它们名称相同,但是入参不同
protected void configure(HttpSecurity http) throws Exception protected void configure(AuthenticationManagerBuilder auth) throws Exception 入参为 HttpSecurity 的 configure 可以配置拦截相关的参数 另一个入参为 AuthenticationManagerBuilder,则是用来配置验证相关的参数
...