SpringBoot集成SpringSecurity
本文最后更新于:2 分钟前
记录贴,方便以后查看
导入pom依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <!--security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>2.3.1.RELEASE</version> </dependency>
<!-- thymeleaf整合包 --> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> <version>3.0.4.RELEASE</version> </dependency>
<!--thymeleaf模板引擎--> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency>
|
修改application.properties配置
| spring.thymeleaf.cache=false
|
Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| @Controller public class IndexController {
@RequestMapping({"/","/index"}) public String index(){ return "index"; }
@RequestMapping("/toLogin") public String toLogin(){ return "views/login"; }
@RequestMapping("/level1/{id}") public String level1(@PathVariable("id") int id){ return "views/level1/"+id; }
@RequestMapping("/level2/{id}") public String level2(@PathVariable("id") int id){ return "views/level2/"+id; }
@RequestMapping("/level3/{id}") public String level3(@PathVariable("id") int id){ return "views/level3/"+id; } }
|
新建Config类
| @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override protected void configure(HttpSecurity http) throws Exception {
}
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception{ }
}
|
解析
授权
方法:
| @Override protected void configure(HttpSecurity http) throws Exception { }
|
| http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3");
|
认证
方法:
| @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception{ }
|
添加用户及权限:
| auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("jinan").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3") .and() .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3") .and() .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1"); }
|
将密码进行编码:
| auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .password(new BCryptPasswordEncoder().encode("123456"))
|
登录功能
定制登录页
- ````java
http.formLogin().loginPage(“/toLogin”); | 2. ````java http.formLogin().loginPage("/toLogin").usernameParameter("user").passwordParameter("psd").loginProcessingUrl("/login");
|
注销功能
- ````java
//注销成功后跳转到首页
http.logout().logoutSuccessUrl(“/“); | 2. ````java http.csrf().disable();
|
记住我功能
自定义接受前端参数
| http.rememberMe().rememberMeParameter("remember");
|
前端:
| <input type="checkbox" name="remember" /> 记住我
|
前端
导入头文件
| xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
|
判断是否登录
| sec:authorize="!isAuthenticated()" sec:authorize="isAuthenticated()"
|
显示当前登录的用户名
| sec:authentication="name"
|
根据权限显示不同内容
| sec:authorize="hasRole('vip1')"
|
完整代码
html动态显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <!--springboot2.0.9版本支持--> <!--如果未登录--> <div sec:authorize="!isAuthenticated()"> <a class="item" th:href="@{/toLogin}"> <i class="address card icon"></i> 登录 </a> </div>
<!--如果已登录--> <div sec:authorize="isAuthenticated()"> <a class="item"> 用户名:<span sec:authentication="name"></span> </a> </div> <div sec:authorize="isAuthenticated()"> <a class="item" th:href="@{/logout}"> <i class="sign-out icon"></i> 注销 </a> </div>
|
SecurityConfig
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests() .antMatchers("/").permitAll() .antMatchers("/level1/**").hasRole("vip1") .antMatchers("/level2/**").hasRole("vip2") .antMatchers("/level3/**").hasRole("vip3");
http.formLogin().loginPage("/toLogin");
http.formLogin().loginPage("/toLogin").usernameParameter("user").passwordParameter("psd").loginProcessingUrl("/login");
http.csrf().disable(); http.logout().logoutSuccessUrl("/");
http.rememberMe().rememberMeParameter("remember"); }
@Override protected void configure(AuthenticationManagerBuilder auth) throws Exception{ auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("jinan").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3") .and() .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3") .and() .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1"); }
}
|