SpringSecurity环境搭建
-
新建一个项目 选择spring web依赖
-
导入thymeleaf依赖
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency>
-
关闭thymeleaf缓存
-
导入静态资源
-
新建controller包,创建一个RouterController类
package com.cjp.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class RouterController { @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; } }
-
导入security依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
-
新建cofig包–>Security类:
package com.cjp.config; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; //AOP :拦截器 @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(); //注销 跳转至首页 http.logout().logoutSuccessUrl("/"); //注销报错,关闭防网站攻击 http.csrf().disable(); //开启记住我功能 http.rememberMe(); } //认证 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()) .withUser("cjp").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3") .and() .withUser("cs").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2") .and() .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1"); } }
thymeleaf、springSecurity整合
-
导入依赖
<!--thymeleaf、SpringSecurity整合包--> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> <version>3.0.4.RELEASE</version> </dependency>
-
在html中导入命名空间
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
-
有问题需要对springboot版本降级
<spring-boot.version>2.0.9.RELEASE</spring-boot.version>
-
修改index.html,未登录时显示登录按钮,登录后显示用户名和注销按钮
<!--未登录显示--> <div sec:authorize="!isAuthenticated()"> <a class="item" th:href="@{/toLogin}"> <i class="address card icon"></i> 登录 </a> </div> <!--已登录显示--> <div sec:authorize="isAuthenticated()"> / 用户名:<span sec:authentication="name"></span> </div> <div sec:authorize="isAuthenticated()"> <a class="item" th:href="@{/logout}"> <i class="sign-out icon"></i> 注销 </a> </div>
-
对应角色显示对应的功能页面
<div class="column" sec:authorize="hasRole('vip1')"> <div class="ui raised segment"> <div class="ui"> <div class="content"> <h5 class="content">Level 1</h5> <hr> <div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i> Level-1-1</a></div> <div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div> <div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> Level-1-3</a></div> </div> </div> </div> </div>
-
登录页面自定义,修改SecurityConfig 页面:
//没有权限跳转到登录页面 自定义登录页面,需要绑定参数 默认为username和password,同名则不需要绑定 http.formLogin().loginPage("/toLogin").usernameParameter("user").passwordParameter("pwd").loginProcessingUrl("/login"); //注销 跳转至首页 http.logout().logoutSuccessUrl("/"); //防网站攻击 http.csrf().disable(); //开启记住我功能 自定义记住我按钮声明参数 http.rememberMe().rememberMeParameter("remember");
项目结构:
-