SpringBoot整合SpringSecurity ╰+攻爆jí腚メ 2021-06-24 16:12 506阅读 0赞 1.添加相关依赖。 <!--Spring Security和thymeleaf的整合依赖--> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> </dependency> <!--thymeleaf模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--Security安全框架--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> 需要注意的是,这里使用了thymeleaf模板引擎来进行前端页面数据交互,默认的thymeleaf版本太低,需要将它修改为更高版本,否则访问页面将会报错。在properties元素中修改版本。 <properties> <java.version>1.8</java.version> <!--默认thymeleaf版本太低,需要设置为更高版本--> <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version> <thymeleaf-extras-springsecurity4.version>3.0.2.RELEASE</thymeleaf-extras-springsecurity4.version> </properties> 2.创建控制页面跳转的controller。 @Controller public class KungfuController { private final String PREFIX = "pages/"; /** * 欢迎页 * @return */ @GetMapping("/") public String index() { return "welcome"; } /** * 登陆页 * @return */ @GetMapping("/userLogin") public String loginPage() { return PREFIX+"login"; } /** * level1页面映射 * @param path * @return */ @GetMapping("/level1/{path}") public String level1(@PathVariable("path")String path) { return PREFIX+"level1/"+path; } /** * level2页面映射 * @param path * @return */ @GetMapping("/level2/{path}") public String level2(@PathVariable("path")String path) { return PREFIX+"level2/"+path; } /** * level3页面映射 * @param path * @return */ @GetMapping("/level3/{path}") public String level3(@PathVariable("path")String path) { return PREFIX+"level3/"+path; } } 3.创建对应映射路径的前端页面,这里省略详细创建过程。 4.创建SpringSecurity的配置类,该类必须继承**WebSecurityConfigurerAdapter**,并且添加注解\*\*@EnableWebSecurity**开启WebSecurity,重写**configure(HttpSecurity http)**方法定制授权规则,重写**configure(AuthenticationManagerBuilder auth)\*\*方法定义认证规则。 @EnableWebSecurity public class MySecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { //super.configure(http); //定制请求的授权规则 http.authorizeRequests().antMatchers("/").permitAll()//index页面允许所有人访问 .antMatchers("/level1/**").hasRole("VIP1")//level1下面的所有页面必须是VIP1才能访问 .antMatchers("/level2/**").hasRole("VIP2")//level2下面的所有页面必须是VIP2才能访问 .antMatchers("/level3/**").hasRole("VIP3");//level3下面的所有页面必须是VIP3才能访问 //开启自动配置登录功能,如果没有权限,就会来到登录页面 //http.formLogin(); // /login来到登录页 //重定向到/login?error表示登录失败 //更多详细规则查看Spring官网 //定制登录页 //默认post形式的/login代表处理登录 //一旦定制loginpage,那么loginPage的post请求就是登录 http.formLogin().usernameParameter("user").passwordParameter("pwd") .loginPage("/userLogin"); //.loginProcessingUrl("");//自定义登录url,默认是loginPage中的url //开启自动配置的注销功能 //http.logout(); //访问/logout表示用户注销,清空Session http.logout().logoutSuccessUrl("/");//注销成功以后来到首页 //开启记住我功能 //http.rememberMe(); //登录成功后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登录 //点击注销会删除cookie //定制登录页时,可以指定记住我的checkbox的name属性,默认叫rememberMe http.rememberMe().rememberMeParameter("remeber"); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //super.configure(auth); //定义认证规则(用户名、密码,权限) auth.inMemoryAuthentication() .withUser("zhangsan").password("123456").roles("VIP1","VIP2") .and() .withUser("lisi").password("123456").roles("VIP2","VIP3") .and() .withUser("wangwu").password("123456").roles("VIP1","VIP3"); } } SpringSecurity的更多详细规则可以查看官网:[https://spring.io/projects/spring-security][https_spring.io_projects_spring-security] [https_spring.io_projects_spring-security]: https://spring.io/projects/spring-security
还没有评论,来说两句吧...