Spring Security 5

对于 Spring Security 5,加入了许多新选项,在密码上的更是添加了新保护,下面分享两种情况下的使用。

注解

@PreAuthorize可以用来控制一个方法是否能够被调用。(在方法执行前执行)
例如在方法上加

// 权限为使用者或者管理员
@PreAuthorize("hasRole('ROLE_USER') or hasRole('ROLE_ADMIN')")
// id 小于 10
@PreAuthorize("#id<10")
// 只能是自己的信息
@PreAuthorize("principal.username.equals(#username)")

@PostAuthorize 方法调用完之后进行权限检查。(在方法执行之后执行)
根据检查权限的结果决定是否要抛出 AccessDeniedException。

// 返回的使用者的 id 为偶数
@PostAuthorize("user.id%2==0")

@PreFilter 和 @PostFilter 进行对集合类型进行过滤,移除使对应表达式的结果为 false 的元素。
当 @PreFilter 标注的方法拥有多个集合类型的参数时,需要通过 @PreFilter 的 filterTarget 属性指定当前 @PreFilter 是针对哪个参数进行过滤的。

@PreFilter(filterTarget="userList", value="userId%2==0")
public void delete(List<User> userList, List<Device> deviceList)@PostFilter("userId%2==0")
public List<User> select(){return userService.selectAll();
}

在不连接数据库的情况下

先创建一个继承 WebSecurityConfigurerAdapter 的 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.BCryptPasswordEncoder;@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {//定制授权规则http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/level1/**").hasRole("VIP1").antMatchers("/level2/**").hasRole("VIP2").antMatchers("/level3/**").hasRole("VIP3");//开启自动配置的登陆功能 http.formLogin();http.formLogin().usernameParameter("username").passwordParameter("pwd").loginPage("/userlogin"); //自设登陆请求// "/login" 来的登陆页// "/login?error" 表示登陆失败// 很多...//默认 post 形式的 /login 表示处理登陆//一旦定制 loginPage loginPage 的 post 请求就是登陆//开启启动配置注销功能 http.logout();// "/logout" 注销,清空 session// 注销成功返回首页http.logout().logoutSuccessUrl("/");//开启记住我http.rememberMe();//登陆成功后,将 cookie 发给浏览器,以后访问页面上带上这个 cookie,只要通过验证,则免登陆//点击注销删除 cookie}//定义认证规则 这里与Spring Security 4 有着很大的区别@Overrideprotected void configure(AuthenticationManagerBuilder auth) throws Exception {auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("user1").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1").and().withUser("user2").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1", "VIP2").and().withUser("user3").password(new BCryptPasswordEncoder().encode("123")).roles("VIP1", "VIP2", "VIP3");}/* 配置信息@Autowiredpublic void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");}*/
}

前端登陆界面
注意 th:action 必须为之前 loginPage("") 中所设定的,框架会自动处理好。

 <div align="center"><form th:action="@{/userlogin}" method="post">用户名:<input name="username"/><br>密码:<input name="pwd"><br/><input type="submit" value="登陆"></form></div>

最后,普及一点前端 springsecurity thymeleaf extra的语法

<!--sec:authorize="!isAuthenticated()" 判断是否登录-->
<div sec:authorize="!isAuthenticated()"><h2 align="center">游客您好,如果想****<a th:href="@{/userlogin}">请登录</a></h2>
</div>
<!--<div sec:authentication="principal.authorities" 获得用户权限,一般前面加有 "ROLE_">--><span sec:authentication="name"></span>您好,您的用户权限为<span sec:authentication="principal.authorities"></span><form th:action="@{/logout}" method="post"><input type="submit" value="注销"></form>
</div>
<!--sec:authorize="hasRole('VIP3')" 判断当前用户是否拥有权限"VIP3"-->
<div sec:authorize="hasRole('VIP3')">VIP3才能访问到的东西。
</div>

与数据库相连的情况

注意建表的时候,password 要注意多预留我设的是200位,其实不需要那么多,应为框架自行加密后位数什么的就都变了,数据库中记录的内容也会有所不同,管理员也不知道用户的密码是什么。
先创建一个工具类,用于密码加密的操作

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;public class PasswordEncoderUtil {private static PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); //使用构造方法生成对象/*** 对密码使用BCryptPasswordEncoder加密方式进行加密*/public static String passwordEncoder(String password) {return passwordEncoder.encode(password);}
}

然后创建一个接入接口 UserDetailsService 的类,Dao 层我这里就不写了,很基础的东西。
我在其中添加了一个 private static final String role = “ROLE_”; 这是因为前端页面中用了 hasRole(’’) 和 hasAnyRole(’’),其中要获取检验的权限是需要前缀 “ROLE_”,如此可以解决存入数据库的权限取出后失效的情况,应该也有别的办法,要重写方法,我觉着这样简单,就这么写了。

import com.meetingroom.dao.UserDao;
import com.meetingroom.model.User;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;@Configuration
public class UserDetailService implements UserDetailsService {@Resourceprivate UserDao userDao;private static final String role = "ROLE_";@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {User user = userDao.findUserByName(username);if (user == null){throw new UsernameNotFoundException("用户名不存在");}Collection<GrantedAuthority> authorities = new ArrayList<>();authorities.add(new SimpleGrantedAuthority(role + user.getAuthority().toString()));return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPwd(), authorities);//注意此处 User 是 Security 的}
}

再创建一个继承 WebSecurityConfigurerAdapter 的 Config 类,进行相对应的配置,注意在取密码的时候要出现调整

import org.springframework.beans.factory.annotation.Autowired;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate UserDetailsService detailsService;@Overrideprotected void configure(HttpSecurity http) throws Exception {//定制授权规则http.authorizeRequests().antMatchers("/").permitAll().antMatchers("/using/**").hasAnyRole("staff","administrators").antMatchers("/admin/**").hasRole("administrators");//开启自动配置的登陆功能
//        http.formLogin();http.formLogin().usernameParameter("username").passwordParameter("pwd").loginPage("/login"); //自设登陆请求//默认 post 形式的 /login 表示处理登陆//一旦定制 loginPage loginPage 的 post 请求就是登陆//开启启动配置注销功能 http.logout();// "/logout" 注销,清空 session// 注销成功返回首页http.logout().logoutSuccessUrl("/");//开启记住我http.rememberMe();}//在数据库中存的密码也是要经过这个加密的才能匹配上,所以需要通过框架插入,而非直接操作数据库@Overridepublic void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(detailsService).passwordEncoder(new BCryptPasswordEncoder());}
}

登陆界面

 <div align="center"><a th:href="@{/signup}">游客注册</a><form th:action="@{/login}" method="post">用户名:<input name="username"/><br>密码:<input name="pwd"><br/><input type="submit" value="登陆"></form></div>

首页

<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>
<a th:href="@{#}">查询</a>
<div sec:authorize="hasAnyRole('staff','administrators')"><a th:href="@{/using/choose}">查看预约情况</a>
</div>
<div sec:authorize="hasRole('administrators')"><a th:href="@{/admin/modify}">修改会议室情况</a>
</div>

Spring Security 5相关推荐

  1. spring boot整合spring security笔记

    最近自己做了一个小项目,正在进行springboot和spring Security的整合,有一丢丢的感悟,在这里分享一下: 首先,spring boot整合spring security最好是使用T ...

  2. Spring Security 实战干货:自定义异常处理

    Spring Security 实战干货:自定义异常处理 转自:https://www.cnblogs.com/felordcn/p/12142514.html 文章目录 1. 前言 2. Sprin ...

  3. Spring security防止跨站请求伪造(CSRF防护)

    因为使用了spring security 安全性框架 所以spring security 会自动拦截站点所有状态变化的请求(非GET,HEAD,OPTIONS和TRACE的请求),防止跨站请求伪造(C ...

  4. 【Spring Security】五、自定义过滤器

    在之前的几篇security教程中,资源和所对应的权限都是在xml中进行配置的,也就在http标签中配置intercept-url,试想要是配置的对象不多,那还好,但是平常实际开发中都往往是非常多的资 ...

  5. SpringBoot整合Spring Security

    个人资源与分享网站:http://xiaocaoshare.com/ SpringSecurity核心功能: 认证(你是谁) 授权(你能干什么) 攻击防护(防止伪造身份) 1.pom.xml < ...

  6. spring security remember me实现自动登录

    1 默认策略 在我们自定义的login中增加一个选择框 <input type="submit" value="Login" /> <br/& ...

  7. Spring security获取当前用户

    1.如果在jsp页面中获取可以使用spring security的标签 页面引入标签 [java] view plain copyprint? <%@ taglib prefix="s ...

  8. Spring Security 之集群Session配置

    1.   新建Maven项目 cluster-session 2.   pom.xml <project xmlns="http://maven.apache.org/POM/4.0. ...

  9. Spring Security 中最流行的权限管理模型!

    前面和大家说了 ACL,讲了理论,也给了一个完整的案例,相信小伙伴们对于 ACL 权限控制模型都已经比较了解了. 本文我要和大家聊一聊另外一个非常流行的权限管理模型,那就是 RBAC. 1.RBAC ...

  10. Spring boot使用Spring Security和OAuth2保护REST接口

    在本文中,我将会演示SpringSecurity+OAuth2如何去保护SpringBoot上的RESTAPI端点,以及客户端和用户凭证如何存储在关系数据库中.因此,我们需要做到以下几点: 配置Spr ...

最新文章

  1. 台式计算机如何自动关机,台式机如何设置自动关机
  2. mysql my-small.ini_MySql优化之my-small.ini配置
  3. java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)
  4. 汇编:分段函数的值的计算
  5. 机器视觉:嵌入式视觉系统中的接口
  6. oracle9i怎样管理数据,数据库教程
  7. 多媒体封装格式的比较
  8. 基于稀疏表示字典学习的图像超分辨率-杨建超论文解析
  9. 桌面计算机快捷打不开,桌面快捷方式打不开,小编教你桌面快捷方式打不开怎么解决...
  10. aspectsof的意思_aspect of是什么意思
  11. 如何在IGV上使用BLAT搜索非模式物种
  12. 等比缩放公式_iPhone屏幕适配,等比缩放
  13. 中文名字和英文名字正则匹配
  14. 突然觉得一个感情空虚的人是多么的痛苦?
  15. 不知道自己不知道 知道自己不知道 不知道自己知道 知道自己知道
  16. (详细)爬虫可视化温州11年天气大作业
  17. python基础教程 excel_python基础教程 excel-python怎么读写excel文件
  18. 我的世界linux版账号密码忘了怎么办,【Linux版本】PocketMine-MP服务器安装
  19. Android 5.0 GET_TASKS
  20. Ubuntu安装xampp和joomla(希望大家少走弯路)

热门文章

  1. 软考之软件评测师考试详解以及准备篇
  2. 自然语言处理与企业对话系统设计
  3. 【Unity3D进阶4-12】Unity3D 对话系统
  4. linux gz解压 指定目,linux解压tar.gz到指定文件夹或目录
  5. QQ音乐.qmc3文件转换成正常mp3文件
  6. Coreldrawx6(cdrx6)
  7. 信息论Matlab仿真——信源熵
  8. 解决Office 2010 每次打开word时出现配置进度框
  9. PIC16F887 单片机 proteus 红外遥控灯光控制系统
  10. www.050604.pw ub.php,《UFIDA用友软件维护工具》050604版使用说明