玩转SpringBoot安全管理第三期来喽💨
前两期传送门:
💫玩转SpringBoot安全管理:SpringSecurity介绍及入门、自定义用户认证及授权管理、MVC Security安全配置介绍(内存和JDBC身份认证实现)
💫玩转SpringBoot安全管理:SpringSecurity之UserDetailService身份认证
资源也在前两期里有链接🔗
依旧回到WebSecurityConfigurerAdapter类当中,这次我们需要实现它的configure(HttpSecurity http)方法,在这个方法中我们实现用户访问控制
在开始之前呢,我们要把templates下的index.html页面改成下面的页面:
其实也就是把sec:authorize="hasRole(‘common’)"和sec:authorize="hasAuthority(‘ROLE_vip’)"给注释掉了,现在已经不需要用到他了,我们要自定义访问控制路径
DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>影视直播厅title>
head>
<body>
<h1 align="center">欢迎进入电影网站首页h1>
<div sec:authorize="isAnonymous()">
<h2 align="center">游客您好,如果想查看电影<a th:href="@{/userLogin}">请登录a>h2>
div>
<div sec:authorize="isAuthenticated()">
<h2 align="center"><span sec:authentication="name" style="color: #007bff">span>您好,您的用户权限为
<span sec:authentication="principal.authorities" style="color:darkkhaki">span>,您有权观看以下电影h2>
<form th:action="@{/mylogout}" method="post">
<input th:type="submit" th:value="注销" />
form>
div>
<hr>
<div >
<h3>普通电影h3>
<ul>
<li><a th:href="@{/detail/common/1}">飞驰人生a>li>
<li><a th:href="@{/detail/common/2}">夏洛特烦恼a>li>
ul>
div>
<div >
<h3>VIP专享h3>
<ul>
<li><a th:href="@{/detail/vip/1}">速度与激情a>li>
<li><a th:href="@{/detail/vip/2}">猩球崛起a>li>
ul>
div>
body>
html>
SecurityConfig.java
package com.security.config;
import com.security.service.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer;
import org.springframework.security.config.annotation.authentication.configurers.provisioning.JdbcUserDetailsManagerConfigurer;
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;
import javax.sql.DataSource;
/**
* @program: spring security
* @description: security配置类
* @author: xmonster_大魔王
* @create: 2022-08-02 11:41
**/
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Qualifier("dataSource")
@Autowired
private DataSource dataSource;
@Autowired
private UserDetailsServiceImpl userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 设置密码编码器
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder(); auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/detail/common/**").hasRole("common")
.antMatchers("/detail/vip/**").hasRole("vip")
.anyRequest().authenticated()
.and().formLogin();
}
}
解析一下上方代码:
放行"/“请求”/detail/common/**",则需要common权限,vip也是一样的道理
.anyRequest().authenticated()的意思是:除了上面的印射地址以外,需要访问其他的页面都是需要登录的
.formLogin():配置了security默认的登录页面
测试
访问:http://localhost:8080/
点击下方的电影,举个例子,点击夏洛特烦恼:
可以看到,会自动跳转到登录界面
输入正确的并且具备能够观看夏洛特烦恼的用户
可以看到自动跳转到夏洛特烦恼的电影详情页面
再回到电影首页,可以看到它会自动获取当前登录用户信息:
如果现在点击查看vip电影,则会:
会报403错误,表示没有这个权限