SpringSecurity
认证:登录之后识别你是谁
授权:证明你可以干什么
helloworld
1导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2编写springsecurity配置类
@EnableWebSecurity 配置类必须继承WebSecurityConfiguserAdapter
3控制文件权限
package com.atguigu.security.config;
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;
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//super.configure(http);
//定制请求的授权规则
//指定/请求的页面 任何用户都能访问
http.authorizeRequests().antMatchers("/").permitAll()
//level1/下的所有页面 VIP1可以访问
.antMatchers("/level1/**").hasRole("VIP1")
//level2/下的所有页面 VIP2可以访问
.antMatchers("/level2/**").hasRole("VIP2")
//level3/下的所有页面 VIP3可以访问
.antMatchers("/level3/**").hasRole("VIP3");
}
}
进入首页可以 随便点一个文件就会403错误
4开启自动配置登录认证
我们希望点击没有权限的文件 提示登录 而不是报403
http.formLogin().loginPage("/userlogin");
//登录页面默认调到security自带的 我们改一下
5设定账号
生产时使用数据可 这里就写在内存中了方便测试
注意:security5.0以上需要把账户和密码加密
.passwordEncoder(new BCryptPasswordEncoder()
new BCryptPasswordEncoder().encode(“123”)
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder()).withUser("zs").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1","VIP2")
.and()
.passwordEncoder(new BCryptPasswordEncoder()).withUser("ls").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1","VIP3")
.and()
.passwordEncoder(new BCryptPasswordEncoder()) .withUser("ww").password(new BCryptPasswordEncoder().encode("123")).roles("VIP2","VIP3");
}
6开启自动配置注销功能
http.logout().logoutSuccessUrl("/");
//退出成功默认跳转到登录页面 我们设置为首页
7登陆成功之后在显示注销按钮
8登陆之后没有权限查看的连接隐藏
1导包
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
2导入名称空间
xmlns:th="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
3
<!DOCTYPE html>
<html 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>Insert title here</title>
</head>
<body>
<!--如果没登录-->
<div sec:authorize="!isAuthenticated()">
<h2 align="center">游客您好,如果想查看武林秘籍 <a th:href="@{/login}">请登录</a></h2>
</div>
<!--如果登陆了-->
<div sec:authorize="isAuthenticated()">
<h2><span sec:authentication="name"></span>
<span sec:authentication="principal.authorities"></span></h2>
<form th:action="@{/logout}" method="post">
<input type="submit" value="注销">
</form>
</div>
<h1 align="center">欢迎光临武林秘籍管理系统</h1>
<hr>
<div sec:authorize="hasRole('VIP1')">
<h3>普通武功秘籍</h3>
<ul>
<li><a th:href="@{/level1/1}">罗汉拳</a></li>
<li><a th:href="@{/level1/2}">武当长拳</a></li>
<li><a th:href="@{/level1/3}">全真剑法</a></li>
</ul>
</div>
<!--如果是VIP2 显示哪些内容-->
<div sec:authorize="hasRole('VIP2')">
<h3>高级武功秘籍</h3>
<ul>
<li><a th:href="@{/level2/1}">太极拳</a></li>
<li><a th:href="@{/level2/2}">七伤拳</a></li>
<li><a th:href="@{/level2/3}">梯云纵</a></li>
</ul>
</div>
<!--如果是VIP3显示哪些-->
<div sec:authorize="hasRole('VIP3')">
<h3>绝世武功秘籍</h3>
<ul>
<li><a th:href="@{/level3/1}">葵花宝典</a></li>
<li><a th:href="@{/level3/2}">龟派气功</a></li>
<li><a th:href="@{/level3/3}">独孤九剑</a></li>
</ul>
</div>
</body>
</html>
开启记住我功能
http.rememberMe();
登陆成功以后将cookiee发给浏览器保存 以后登录就带上这个cookie 如果点击注销会删除cookie
最终config代码
package com.atguigu.security.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;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.lang.reflect.AnnotatedArrayType;
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//super.configure(http);
//定制请求的授权规则
//指定/请求的页面 任何用户都能访问
http.authorizeRequests().antMatchers("/").permitAll()
//level1/下的所有页面 VIP1可以访问
.antMatchers("/level1/**").hasRole("VIP1")
//level2/下的所有页面 VIP2可以访问
.antMatchers("/level2/**").hasRole("VIP2")
//level3/下的所有页面 VIP3可以访问
.antMatchers("/level3/**").hasRole("VIP3");
http.formLogin().loginPage("/userlogin");
//开启自动配置的注销功能 访问/logout表示用户注销清空session
http.logout().logoutSuccessUrl("/");//退出成功默认跳转到登录页面 我们设置为首页
//开启自动记住我功能
http.rememberMe();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder()).withUser("zs").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1","VIP2")
.and()
.passwordEncoder(new BCryptPasswordEncoder()).withUser("ls").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1","VIP3")
.and()
.passwordEncoder(new BCryptPasswordEncoder()) .withUser("ww").password(new BCryptPasswordEncoder().encode("123")).roles("VIP2","VIP3");
}
}