目录
- 一、SpringBoot整合SpringSecurity
- 1、pom文件
- 2、静态资源
- 3、MySecurityController
- 4、自定义SpringSecurity的配置类(
重点
)
org.thymeleaf.extras
thymeleaf-extras-springsecurity5
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
2、静态资源
跳转到目录 1.html, 其他x.html都一样
welcome.html
Insert title here
欢迎光临武林秘籍管理系统
游客您好,如果想查看武林秘籍 请登录
,您好,您的角色有:
普通武功秘籍
罗汉拳
武当长拳
全真剑法
高级武功秘籍
太极拳
七伤拳
梯云纵
绝世武功秘籍
葵花宝典
龟派气功
独孤九剑
login.html
Insert title here
欢迎登陆武林秘籍管理系统
用户名:
密码:
记住我
3、MySecurityController
跳转到目录
@Controller
public class MySecurityController {
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;
}
}
4、自定义SpringSecurity的配置类(重点
)
跳转到目录
@EnableWebSecurity //开启WebSecurity模式
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//super.configure(http);
//定制请求的授权规则
// 不同的角色可以访问的权限不同
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//开启自动配置的登陆功能,效果,如果没有登陆,没有权限就会来到登陆页面
//1、/login来到登陆页
//2、重定向到/login?error表示登陆失败
//3、更多详细规定
//4、默认post形式的 /login代表处理登陆
//5、一但定制loginPage;那么 loginPage的post请求 /userlogin就代表处理登陆
http.formLogin().usernameParameter("user").passwordParameter("pwd")
.loginPage("/userlogin");
//开启自动配置的注销功能。
//1、访问 /logout 表示用户注销,清空session
//2、注销成功会返回 /login?logout 页面;
http.logout().logoutSuccessUrl("/");//设置注销成功以后来到首页
//开启记住我功能
//登陆成功以后,将cookie发给浏览器保存,以后访问页面带上这个cookie,只要通过检查就可以免登录
//点击注销会删除cookie
http.rememberMe().rememberMeParameter("remeber");
}
//定义认证规则
// 需要对密码进行加密 PasswordEncoder
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 这些数据正常应该从数据库中读
// 给不同的用户设置不同的角色
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("zhangsan").password(new BCryptPasswordEncoder().encode("123")).roles("vip2", "vip3")
.and()
.withUser("lisi").password(new BCryptPasswordEncoder().encode("123")).roles("vip1")
.and()
.withUser("zy").password(new BCryptPasswordEncoder().encode("123")).roles("vip1", "vip2", "vip3");
}
}