本文共 2780 字,大约阅读时间需要 9 分钟。
1.8 3.0.9.RELEASE 2.2.2
org.thymeleaf.extras thymeleaf-extras-springsecurity4 3.0.2.RELEASE org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-security
package com.example.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;@EnableWebSecuritypublic class MySecurityConfig extends WebSecurityConfigurerAdapter{ @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/").permitAll() //访问任何资源都转跳到该路径,即首页 .antMatchers("/level1/**").hasRole("VIP1") //访问 **/level1/**路径下的资源都需要角色 VIP1, 相当于session一样; 如 page/level1/1 .antMatchers("/level2/**").hasRole("VIP2") .antMatchers("/level3/**").hasRole("VIP3"); // http.formLogin(); //当要跳转需要权限的以上资源或者"/login"时会跳转到默认登录界面 http.csrf().disable(); //将csrf关闭 http.formLogin().loginPage("/MyLogin"); //跳转到自己制定的登陆页面 http.formLogin().defaultSuccessUrl("/"); //登录成功跳转的页面 http.formLogin().failureUrl("/aaa"); //登录失败后跳转到自己制定的登陆失败页面 http.formLogin().loginProcessingUrl("/message"); //设置自定义页面form表单action路径 /* * 1、用户注销, 清空session * 2、注销成功会返回/login?logout 页面(默认) */ http.logout().logoutSuccessUrl("/"); //设置注销成功后返回的页面, 即首页 //开启 记住我, 并设置 checkbox 的 name, 向浏览器提交一个 cookie http.rememberMe().rememberMeParameter("remember"); System.out.println("WebSecurity设置成功..."); } //授权角色信息 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { //将三个用户信息保存在内存中 auth.inMemoryAuthentication().withUser("huang").password("admin").roles("VIP1", "VIP2") .and().withUser("he").password("admin").roles("VIP2", "VIP3") .and().withUser("he2").password("admin").roles("VIP3", "VIP4"); } }
第一个HTML页面 Title 自定义表单验证:
Insert title here 欢迎光临武林秘籍管理系统
游客您好,如果想查看武林秘籍 请登录
,您好,您的角色有:
转载地址:http://jyern.baihongyu.com/