一:实现授权认证
自己先建一个springboot项目,这里就先不建了
1.导入静态资源模板
链接:https://pan.baidu.com/s/143hu-b_6dLKKqMOs05Ze8Q
提取码:iv3t
记得把view放到templates下面
2.在maven中引入thymeleaf模板和secrity的依赖包
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3.建立一个controllerl类
@Controller
public class tsestController{
@RequestMapping("/index")
public String in(){
return "index";
}
@RequestMapping("/toLogin")
public String login(){
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;
}
}
4.建立一个 Secrityconfig类继承自WebSecurityConfigurerAdapter
public class Secrityconfig extends WebSecurityConfigurerAdapter{
/**
* 授权
* @param http
* @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");
//没有权限默认会到登录页面,需要开启登录的页面
http.formLogin()
}
/**
* 认证
* @param auth
* @throws Exception
*/
//在内存中定义,也可以在jdbc中去拿....
//Spring security 5.0中新增了多种加密方式,也改变了密码的格式。
//要想我们的项目还能够正常登陆,需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密
//spring security 官方推荐的是使用bcrypt加密方式。
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("kuangshen").password( new
BCryptPasswordEncoder().encode("111")).roles("vip1","vip2")
.and()
.withUser("zhang").password(new
BCryptPasswordEncoder().encode("123456")).roles("vip3")
.and()
.withUser("chuanxin").password(new
BCryptPasswordEncoder().encode("1")).roles("vip1","vip3");
}
二: 注销及权限控制
先在maven中导入thymeleaf和secrity的中间包
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
在 Secrityconfig 中写上
//开启注销
http.logout();
//注销成功返回首页
http.logout().logoutSuccessUrl("/index");
在前端index页面加上
xmlns:sec=“http://www.thymeleaf.org/thymeleaf-extras-springsecurity5”
实现没登录显示登录,登录显示注销及其角色和用户名
<!--登录注销-->
<div class="right menu">
<!--未登录-->
<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">
<i class="address card icon"></i>
用户名:<span sec:authentication="principal.username"></span>
角色:<span sec:authentication="principal.authorities"></span>
</a>
</div>
<div sec:authorize="isAuthenticated()">
<a class="item" th:href="@{/logout}">
<i class="address card icon"></i> 注销
</a>
</div>