Python 官方文档:入门教程 => 点击学习
目录DSL配置风格为什么这么灵活?使用方法普通登录验证码登录小程序登录最近实现了一个多端登录的spring Security组件,用起来非常丝滑,开箱即用,可插拔,而且灵活性非常强。
最近实现了一个多端登录的spring Security组件,用起来非常丝滑,开箱即用,可插拔,而且灵活性非常强。我觉得能满足大部分场景的需要。目前完成了手机号验证码和微信小程序两种自定义登录,加上默认的FORM登录,一共三种,现在开源分享给大家,接下来简单介绍一下这个插件包。
切入正题,先来看看配置:
@Bean
SecurityFilterChain defaultSecurityFilterChain(httpsecurity Http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.mvcMatchers("/foo/**")
.access("hasAuthority('ROLE_USER')").anyRequest().authenticated()
.and()
// 默认form表单登录
.formLogin()
.and()
.apply(new LoginFilterSecurityConfigurer<>())
// 验证码登录
.captchaLogin(captchaLoginConfigurer ->
// 验证码校验 1 在此处配置 优先级最高 2 注册为Spring Bean 可以免配置
captchaLoginConfigurer.captchaService(this::verifyCaptchaMock)
// 根据手机号查询用户UserDetials 1 在此处配置 优先级最高 2 注册为Spring Bean 可以免配置
.captchaUserDetailsService(this::loadUserByPhoneMock)
// 生成Jwt 返回 1 在此处配置 优先级最高 2 注册为Spring Bean 可以免配置
.jwtTokenGenerator(this::tokenResponseMock)
//todo 其它配置省略……
)
// 小程序登录 同时支持多个小程序
.miniAppLogin(miniAppLoginConfigurer -> miniAppLoginConfigurer
// 实现小程序多租户
// 根据请求携带的clientid 查询小程序的appid和secret 1 在此处配置 优先级最高 2 注册为Spring Bean 可以免配置
.miniAppClientService(this::miniAppClientMock)
// 小程序用户 自动注册和检索 1 在此处配置 优先级最高 2 注册为Spring Bean 可以免配置
.miniAppUserDetailsService(new MiniAppUserDetailsServiceMock())
// 小程序sessionkey缓存 过期时间应该小于微信官方文档的声明 1 在此处配置 优先级最高 2 注册为Spring Bean 可以免配置
.miniAppSessionKeyCache(new MiniAppSessionKeyCacheMock())
// 生成JWT 返回 1 在此处配置 优先级最高 2 注册为Spring Bean 可以免配置
.jwtTokenGenerator(this::tokenResponseMock)
//todo 其它配置省略……
);
return http.build();
}
这种风格完全贴合了Spring Security的DSL配置风格,不仅仅高大上,而且可以按需配置。如果你没有验证码登录直接删掉captchaLogin
方法;如果你没有微信小程序登录直接删掉miniAppLogin
方法。甚至还可以对单种登录进行细粒度定制化,formLogin
有的功能基本验证码登录和微信小程序登录的都有。
这里抽象了一个登录配置类:
public abstract class AbstractLoginFilterConfigurer<H extends HttpSecurityBuilder<H>,
C extends AbstractLoginFilterConfigurer<H, C, F>,
F extends AbstractAuthenticationProcessingFilter>
extends AbstractHttpConfigurer<AbstractLoginFilterConfigurer<H, C, F>, H> {
// 省略……
}
所有额外的登录渠道大都可以通过这个类来扩展,负责验证码登录的CaptchaLoginFilterConfigurer
和微信小程序登录的MiniAppLoginFilterConfigurer
都是该类实现的,基本上你看了源码也能照葫芦画瓢来一个。
另外上面这些配置项接口,都可以放在Spring ioc中,配置类能自动获取,不过优先级最高的还是通过上面代码中配置的具体实现,原理参见下面的的样例:
@Override
protected AuthenticationSuccesshandler defaultSuccessHandler(H http) {
// 如果配置类没有配置 就尝试去Spring IoC中发现
if (this.jwtTokenGenerator == null) {
ApplicationContext applicationContext = http.getSharedObject(ApplicationContext.class);
jwtTokenGenerator = getBeanOrNull(applicationContext, JwtTokenGenerator.class);
}
Assert.notNull(jwtTokenGenerator, "jwtTokenGenerator is required");
return new LoginAuthenticationSuccessHandler(jwtTokenGenerator);
}
public final <T> T getBeanOrNull(ApplicationContext applicationContext, Class<T> beanType) {
String[] beanNames = applicationContext.getBeanNamesForType(beanType);
if (beanNames.length == 1) {
return applicationContext.getBean(beanNames[0], beanType);
}
return null;
}
自行使用Maven命令mvn install
到本地仓库,然后引入:
<dependency>
<groupId>cn.felord</groupId>
<artifactId>spring-security-extension</artifactId>
<version>1.0.0</version>
</dependency>
原生Spring Security接口
POST /login?username=user&passWord=12345 HTTP/1.1
Host: localhost:8080
需要先实现必须的配置接口
发送验证码后调用验证码登录接口:
POST /login/captcha?phone=11111111111&captcha=123123 HTTP/1.1
Host: localhost:8080
需要先实现必须的配置接口
前端先调用微信授权登录接口获取openid
:
POST /miniapp/preauth?clientId=wxxda23234&jsCode=051A23234ZHa1tZ5yj3AOlFr HTTP/1.1
Host: localhost:8080
响应:
{
"code": 200,
"data": {
"errcode": null,
"errmsg": null,
"sessionKey": null,
"openid": "oWmZj5QBrZxxxxx8OUxRrZJi4",
"uNIOnid": "oS-dxxxxxx4w_x7dA-h9MIuA"
},
"msg": "",
"identifier": true
}
然后调用小程序登录接口:
POST /login/miniapp HTTP/1.1
Host: localhost:8080
Content-Type: application/JSON
{
"clientId": "wxd14qr6",
"openId": "oWmZj5QBrZIBks0xx8OUxRrZJi4",
"unionId": "oS-dK520tgW8xxxx7dA-h9MIuA",
"iv":"LQUOt8BSTa7xxxpe1Q==",
"encryptedData": "10hn3o4xxxxxrO/Ag5nRD3QkLSzduKnWuzN9B/H4Y0G5mDPR8siA7T8yaaqZsrMycLAoe2qrd1J75yYetYuWifiq3jUrcceRZHVxxl9LnQdW8f5+pMTnQtCYiMJ7Jm9paCw2Bh+5Lowkyqkx1q0fALvCQ9LXPPLAbLOB9CavRfKoenAmyyHQjZ/6lz0njzA=="
}
获取方式
gitee: felord/spring-security-login-extension
到此这篇关于Spring Security 一键接入验证码登录和小程序登录的文章就介绍到这了,更多相关Spring Security 一键接入登录内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringSecurity组件一键接入验证码登录和小程序登录的详细过程
本文链接: https://www.lsjlt.com/news/144977.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0