关于Boot应用中集成Spring Security你必须了解的那些事
<h4>Spring Security</h4>
Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架。除了常规的Authentication和Authorization之外,Spring Security还提供了诸如ACLs,LDAP,JAAS,CAS等高级特性以满足复杂场景下的安全需求。虽然功能强大,Spring Security的配置并不算复杂(得益于官方详尽的文档),尤其在3.2版本加入Java Configuration的支持之后,可以彻底告别令不少初学者望而却步的XML Configuration。在使用层面,Spring Security提供了多种方式进行业务集成,包括注解,Servlet API,JSP Tag,系统API等。下面就结合一些示例代码介绍Boot应用中集成Spring Security的几个关键点。
1 核心概念
Principle(User), Authority(Role)和Permission是Spring Security的3个核心概念。跟通常理解上Role和Permission之间一对多的关系不同,在Spring Security中,Authority和Permission是两个完全独立的概念,两者并没有必然的联系,但可以通过配置进行关联。
2 基础配置
首先在项目的pom.xml中引入spring-boot-starter-security依赖。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>
和其余Spring框架一样,XML Configuration和Java Configuration是Spring Security的两种常用配置方式。Spring 3.2版本之后,Java Configuration因其流式API支持,强类型校验等特性,逐渐替代XML Configuration成为更广泛的配置方式。下面是一个示例Java Configuration。
@Configuration@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true)public class SecurityConfig extends WebSecurityConfigurerAdapter {@AutowiredMyUserDetailsService detailsService;@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests().and().formLogin().loginPage("/login").permitAll().defaultSuccessUrl("/", true).and().logout().logoutUrl("/logout").and().sessionManagement().maximumSessions(1).expiredUrl("/expired").and().and().exceptionHandling().accessDeniedPage("/accessDenied");}@Overridepublic void configure(WebSecurity web) throws Exception {web.ignoring().antMatchers("/js/**", "/css/**", "/images/**", "/**/favicon.ico");}@Overridepublic void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(detailsService).passwordEncoder(new BCryptPasswordEncoder());}}
- @EnableWebSecurity: 禁用Boot的默认Security配置,配合@Configuration启用自定义配置(需要扩展WebSecurityConfigurerAdapter)
- @EnableGlobalMethodSecurity(prePostEnabled = true): 启用Security注解,例如最常用的@PreAuthorize
- configure(HttpSecurity): Request层面的配置,对应XML Configuration中的
<http>元素 - configure(WebSecurity): Web层面的配置,一般用来配置无需安全检查的路径
- configure(AuthenticationManagerBuilder): 身份验证配置,用于注入自定义身份验证Bean和密码校验规则
3 扩展配置
完成基础配置之后,下一步就是实现自己的UserDetailsService和PermissionEvaluator,分别用于自定义Principle, Authority和Permission。
@Componentpublic class MyUserDetailsService implements UserDetailsService {@Autowiredprivate LoginService loginService;@Autowiredprivate RoleService roleService;@Overridepublic UserDetails loadUserByUsername(String username) {if (StringUtils.isBlank(username)) {throw new UsernameNotFoundException("用户名为空");}Login login = loginService.findByUsername(username).orElseThrow(() -> new UsernameNotFoundException("用户不存在"));Set<GrantedAuthority> authorities = new HashSet<>();roleService.getRoles(login.getId()).forEach(r -> authorities.add(new SimpleGrantedAuthority(r.getName())));return new org.springframework.security.core.userdetails.User(username, login.getPassword(),true,//是否可用true,//是否过期true,//证书不过期为truetrue,//账户未锁定为trueauthorities);}}
创建GrantedAuthority对象时,一般名称加上ROLE_前缀。
@Componentpublic class MyPermissionEvaluator implements PermissionEvaluator {@Autowiredprivate LoginService loginService;@Autowiredprivate RoleService roleService;@Overridepublic boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {String username = authentication.getName();Login login = loginService.findByUsername(username).get();return roleService.authorized(login.getId(), targetDomainObject.toString(), permission.toString());}@Overridepublic boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {// not supportedreturn false;}}
- hasPermission(Authentication, Object, Object)和hasPermission(Authentication, Serializable, String, Object)两个方法分别对应Spring Security中两个同名的表达式。
4 业务集成
Spring Security提供了注解,Servlet API,JSP Tag,系统API等多种方式进行集成,最常用的是第一种方式,包含@Secured, @PreAuthorize, @PreFilter, @PostAuthorize和@PostFilter五个注解。@Secure是最初版本中的一个注解,自3.0版本引入了支持Spring EL表达式的其余四个注解之后,就很少使用了。
@RequestMapping(value = "/hello", method = RequestMethod.GET)@PreAuthorize("authenticated and hasPermission('hello', 'view')")public String hello(Model model) {String username = SecurityContextHolder.getContext().getAuthentication().getName();model.addAttribute("message", username);return "hello";}
- @PreAuthorize("authenticated and hasPermission('hello', 'view')"): 表示只有当前已登录的并且拥有("hello", "view")权限的用户才能访问此页面
- SecurityContextHolder.getContext().getAuthentication().getName(): 获取当前登录的用户,也可以通过HttpServletRequest.getRemoteUser()获取
总结
以上就是Spring Security的一般集成步骤,更多细节和高级特性可参考官方文档。
参考
- http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
- http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/
http://emacoo.cn/blog/spring-boot-security
</div>
关于Boot应用中集成Spring Security你必须了解的那些事的更多相关文章
- 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
- Spring Boot中集成Spring Security 专题
check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...
- spring boot rest 接口集成 spring security(2) - JWT配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring boot rest 接口集成 spring security(1) - 最简配置
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
- spring-boot-starter-security Spring Boot中集成Spring Security
spring security是springboot支持的权限控制系统. security.basic.authorize-mode 要使用权限控制模式. security.basic.enabled ...
- SpringBoot 集成Spring security
Spring security作为一种安全框架,使用简单,能够很轻松的集成到springboot项目中,下面讲一下如何在SpringBoot中集成Spring Security.使用gradle项目管 ...
- SpringBoot集成Spring Security
1.Spring Security介绍 Spring security,是一个强大的和高度可定制的身份验证和访问控制框架.它是确保基于Spring的应用程序的标准 --来自官方参考手册 Spring ...
- Spring Boot中使用 Spring Security 构建权限系统
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,为应用系统提供声明式的安全 ...
- Spring Boot 集成 Spring Security 实现权限认证模块
作者:王帅@CodeSheep 写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...
随机推荐
- keras中的shape/input_shape
在keras中,数据是以张量的形式表示的,张量的形状称之为shape,表示从最外层向量逐步到达最底层向量的降维解包过程.“维”的也叫“阶”,形状指的是维度数和每维的大小.比如,一个一阶的张量[1,2, ...
- 一个典型的flex布局,兼容性比较好
html 代码: <body class="flex-wrap col-flex"> <header class="midCenter flex-wra ...
- python 实例方法,类方法,静态方法,普通函数
python中有实例方法,类方法,静态方法,普通函数 类方法需要@ classmethod 修饰并且有个隐藏参数 cls,实例方法必须有个参数 self, 静态方法必须有 @staticmethod修 ...
- 爬虫_python3_requests
Requests 网络资源(URLs)撷取套件 改善Urllib2的缺点,让使用者以最简单的方式获取网络资源 可以使用REST操作(POST,PUT,GET,DELETE)存取网络资源 import ...
- 解决VS2013无法安装ArcObjects10.2的问题
之前在网上看到的10.1在vs2012安装不上的问题,解决办法是改注册表HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\1 ...
- (51)zabbix命令:zabbix_get获取item数据
zabbix_get是什么?有什么作用? 总有人在群里提问,为什么zabbix获取不到数据,为什么zabbix提示Not Support,怎么办?别老问,用zabbix_get试着获取数据即可.在za ...
- lsof指令使用简介
lsof替代了netstat和ps的全部工作.它可以带来那些工具所能带来的一切,而且要比那些工具多得多 最重要的是,当你给它传递选项时,默认行为是对结果进行“或”运算.因此,如果是用-i来拉出一个端口 ...
- C语言:哲学家吃饭问题
//五个哲学家围坐在一起,两人之间都放有一个叉子,意大利面需要2个叉子吃,哲学家吃饭时候叉子只能拿左右手,哲学家除了吃饭时间其他时间都在思考 #include <stdio.h> #inc ...
- The twelve Day-前端之html
前端知识之html内容 HTML介绍 1.web服务本质 import socket sk = socket.socket() sk.bind(()) sk.listen() while True: ...
- 异步FIFO最小深度计算
计算FIFO深度是设计FIFO中常遇到的问题.常识告诉我们,当读速率慢于写速率时,FIFO便可被用作系统中的缓冲元件或队列.因此FIFO的大小基本上暗示了所需缓存数据的容量,该容量取决于读写数据的速率 ...