1. 前言

欢迎阅读 Spring Security 实战干货 系列文章 。最近有开发小伙伴提了一个有趣的问题。他正在做一个项目,涉及两种风格,一种是给小程序出接口,安全上使用无状态的JWT Token;另一种是管理后台使用的是Freemarker,也就是前后端不分离的Session机制。用Spring Security该怎么办?

2. 解决方案

我们可以通过多次继承WebSecurityConfigurerAdapter构建多个HttpSecurityHttpSecurity 对象会告诉我们如何验证用户的身份,如何进行访问控制,采取的何种策略等等。

如果你看过之前的教程,我们是这么配置的:

/**
* 单策略配置
*
* @author felord.cn
* @see org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration
* @since 14 :58 2019/10/15
*/
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)
@EnableWebSecurity
@ConditionalOnClass(WebSecurityConfigurerAdapter.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class CustomSpringBootWebSecurityConfiguration { /**
* The type Default configurer adapter.
*/
@Configuration
@Order(SecurityProperties.BASIC_AUTH_ORDER)
static class DefaultConfigurerAdapter extends WebSecurityConfigurerAdapter { @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
super.configure(auth);
} @Override
public void configure(WebSecurity web) {
super.configure(web);
} @Override
protected void configure(HttpSecurity http) throws Exception {
// 配置 httpSecurity }
}
}

上面的配置了一个HttpSecurity,我们如法炮制再增加一个WebSecurityConfigurerAdapter的子类来配置另一个HttpSecurity。伴随而来的还有不少的问题要解决。

2.1 如何路由不同的安全配置

我们配置了两个HttpSecurity之后,程序如何让小程序接口和后台接口走对应的HttpSecurity

HttpSecurity.antMatcher(String antPattern)可以提供过滤机制。比如我们配置:

     @Override
protected void configure(HttpSecurity http) throws Exception {
// 配置 httpSecurity
http.antMatcher("/admin/v1"); }

那么该HttpSecurity将只提供给以/admin/v1开头的所有URL。这要求我们针对不同的客户端指定统一的URL前缀。

举一反三只要HttpSecurity提供的功能都可以进行个性化定制。比如登录方式,角色体系等。

2.2 如何指定默认的HttpSecurity

我们可以通过在WebSecurityConfigurerAdapter实现上使用@Order注解来指定优先级,数值越大优先级越低,没有@Order注解将优先级最低。

2.3 如何配置不同的UserDetailsService

很多情况下我们希望普通用户和管理用户完全隔离,我们就需要多个UserDetailsService,你可以在下面的方法中对AuthenticationManagerBuilder进行具体的设置来配置UserDetailsService,同时也可以配置不同的密码策略。

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// 自行实现
return null ;
}
});
// 也可以设计特定的密码策略
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder);
auth.authenticationProvider(daoAuthenticationProvider);
}

2.4 最终的配置模板

上面的几个问题解决之后,我们基本上掌握了在一个应用中执行多种安全策略。配置模板如下:

/**
* 多个策略配置
*
* @author felord.cn
* @see org.springframework.boot.autoconfigure.security.servlet.SpringBootWebSecurityConfiguration
* @since 14 :58 2019/10/15
*/
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true, jsr250Enabled = true, securedEnabled = true)
@EnableWebSecurity
@ConditionalOnClass(WebSecurityConfigurerAdapter.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class CustomSpringBootWebSecurityConfiguration { /**
* 后台接口安全策略. 默认配置
*/
@Configuration
@Order(1)
static class AdminConfigurerAdapter extends WebSecurityConfigurerAdapter { @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
//用户详情服务个性化
daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// 自行实现
return null;
}
});
// 也可以设计特定的密码策略
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder);
auth.authenticationProvider(daoAuthenticationProvider);
} @Override
public void configure(WebSecurity web) {
super.configure(web);
} @Override
protected void configure(HttpSecurity http) throws Exception {
// 根据需求自行定制
http.antMatcher("/admin/v1")
.sessionManagement(Customizer.withDefaults())
.formLogin(Customizer.withDefaults()); }
} /**
* app接口安全策略. 没有{@link Order}注解优先级比上面低
*/
@Configuration
static class AppConfigurerAdapter extends WebSecurityConfigurerAdapter { @Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
//用户详情服务个性化
daoAuthenticationProvider.setUserDetailsService(new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// 自行实现
return null;
}
});
// 也可以设计特定的密码策略
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
daoAuthenticationProvider.setPasswordEncoder(bCryptPasswordEncoder);
auth.authenticationProvider(daoAuthenticationProvider);
} @Override
public void configure(WebSecurity web) {
super.configure(web);
} @Override
protected void configure(HttpSecurity http) throws Exception {
// 根据需求自行定制
http.antMatcher("/app/v1")
.sessionManagement(Customizer.withDefaults())
.formLogin(Customizer.withDefaults()); }
}
}

3. 总结

今天我们解决了如何针对不同类型接口采取不同的安全策略的方法,希望对你有用,如果你有什么问题可以留言。多多关注:码农小胖哥,更多干货奉上。

关注公众号:Felordcn 获取更多资讯

个人博客:https://felord.cn

Spring Security 实战干货:如何实现不同的接口不同的安全策略的更多相关文章

  1. Spring Security 实战干货:使用 JWT 认证访问接口

    (转载)原文链接:https://my.oschina.net/10000000000/blog/3127268 1. 前言 欢迎阅读Spring Security 实战干货系列.之前我讲解了如何编写 ...

  2. Spring Security 实战干货: 简单的认识 OAuth2.0 协议

    1.前言 欢迎阅读 Spring Security 实战干货 系列文章 .OAuth2.0 是近几年比较流行的授权机制,对于普通用户来说可能每天你都在用它,我们经常使用的第三方登录大都基于 OAuth ...

  3. Spring Security 实战干货:图解Spring Security中的Servlet过滤器体系

    1. 前言 我在Spring Security 实战干货:内置 Filter 全解析对Spring Security的内置过滤器进行了罗列,但是Spring Security真正的过滤器体系才是我们了 ...

  4. Spring Security 实战干货:理解AuthenticationManager

    1. 前言 我们上一篇介绍了UsernamePasswordAuthenticationFilter的工作流程,留下了一个小小的伏笔,作为一个Servlet Filter应该存在一个doFilter实 ...

  5. Spring Security 实战干货:图解用户是如何登录的

    1. 前言 欢迎阅读Spring Security 实战干货系列文章,在集成Spring Security安全框架的时候我们最先处理的可能就是根据我们项目的实际需要来定制注册登录了,尤其是Http登录 ...

  6. Spring Security 实战干货:实现自定义退出登录

    文章目录 1. 前言 2. 我们使用 Spring Security 登录后都做了什么 2. 退出登录需要我们做什么 3. Spring Security 中的退出登录 3.1 LogoutFilte ...

  7. Spring Security 实战干货:OAuth2第三方授权初体验

    1. 前言 Spring Security实战干货系列 现在很多项目都有第三方登录或者第三方授权的需求,而最成熟的方案就是OAuth2.0授权协议.Spring Security也整合了OAuth2. ...

  8. Spring Security 实战干货:客户端OAuth2授权请求的入口

    1. 前言 在Spring Security 实战干货:OAuth2第三方授权初体验一文中我先对OAuth2.0涉及的一些常用概念进行介绍,然后直接通过一个DEMO来让大家切身感受了OAuth2.0第 ...

  9. Spring Security 实战干货:OAuth2授权请求是如何构建并执行的

    在Spring Security 实战干货:客户端OAuth2授权请求的入口中我们找到了拦截OAuth2授权请求入口/oauth2/authorization的过滤器OAuth2Authorizati ...

随机推荐

  1. webpack指南(三)缓存

    缓存 把 /dist 目录中的内容部署到服务器上,客户端(通常是浏览器)就能够访问网站此服务器的网站及其资源.而通过网络获取资源是比较耗时的,这就是为什么浏览器要使用缓存这种技术.可以通过命中缓存,以 ...

  2. python3.x 基础三:字符集问题

    总结了一张表,更详细信息百度百科: 序号 年份 编码 标准协会 特点 二进制长度 字符长度 表现 1 1967 ASCII 美国国家标准学会(American National Standard In ...

  3. 欧拉函数 BZOJ2705

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 3154  Solved: 1968[Submit][ ...

  4. WordPress获取某个分类关联的标签

    我在WordPress后台某篇文章的编辑页面,给这篇文章选择了分类:WordPress,接着同时选择了标签:php.主题制作,这时分类(WordPress)就与标签(php.主题制作)建立了关联,利用 ...

  5. JSP页面基础用法和方法查询

    导包jar:jstl.jar    standard.jar jsp嵌套java代码,使用jsp脚本:1)<%java代码%> ----- 内部的java代码翻译到service方法的内部 ...

  6. 【Java8新特性】Stream API有哪些中间操作?看完你也可以吊打面试官!!

    写在前面 在上一篇<[Java8新特性]面试官问我:Java8中创建Stream流有哪几种方式?>中,一名读者去面试被面试官暴虐!归根结底,那哥儿们还是对Java8的新特性不是很了解呀!那 ...

  7. [JavaWeb基础] 004.用JSP + SERVLET 进行简单的增加删除修改

    上一次的文章,我们讲解了如何用JAVA访问MySql数据库,对数据进行增加删除修改查询.那么这次我们把具体的页面的数据库操作结合在一起,进行一次简单的学生信息操作案例. 首先我们创建一个专门用于学生管 ...

  8. 如何利用 Python 爬虫实现给微信群发新闻早报?(详细)

    1. 场景 经常有小伙伴在交流群问我,每天的早报新闻是怎么获取的? 其实,早期使用的方案,是利用爬虫获取到一些新闻网站的标题,然后做了一些简单的数据清洗,最后利用 itchat 发送到指定的社群中. ...

  9. word dde payload

    payload: ctrl+F9 {DDEAUTO c:\\windows\\system32\\cmd.exe "/k calc.exe" } Since this techni ...

  10. Rocket - debug - DebugCustomXbar

    https://mp.weixin.qq.com/s/7h9Bdb0x4_clyigMU_0B7Q 讨论DebugCustomXbar中的几个问题. 1. sources/sourceParams n ...