spring oauth2 实现用户名密码登录、手机号验证码登录返回token
文章目录
- 介绍
- 实现功能
- 用户名密码登录
- 步骤
- 编写成功处理器
- 配置成功处理器
- 手机号验证码登录
- 步骤
- 重写SmsCodeAuthenticationSecurityConfig
- 测试
- 用户名密码登录
- 手机号验证码登录
- 项目源码
介绍
spring oauth2提供了授权码,密码等模式。登录成功之后返回token。但在app中,需要用户名和密码或者是手机号验证码登录成功之后也返回token。下面将对这两种模式登录成功之后返回token功能。
实现功能
用户名密码或者手机号验证码登录成功之后返回token。
用户名密码登录
步骤
接着上一章的源码继续写。
编写成功处理器
该处理器是用来处理登录成功之后,该怎么返回数据,如果网页请求,不处理。如果是app请求,则返回token。
/*** @author lvhaibao* @description 当用户登录成功之后做的处理* @date 2019/1/8 0008 10:06*/
@Component
@Slf4j
public class MyAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {@Autowiredprivate ClientDetailsService clientDetailsService;@Autowiredprivate AuthorizationServerTokenServices authorizationServerTokenServices;@Autowiredprivate ObjectMapper objectMapper;@Overridepublic void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,Authentication authentication) throws IOException, ServletException {log.info("登录成功之后的处理");String type = request.getHeader("Accept");if(!type.contains("text/html")){String clientId = "app";String clientSecret = "app";ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);if (null == clientDetails) {throw new UnapprovedClientAuthenticationException("clientId不存在" + clientId);} else if (!StringUtils.equals(clientDetails.getClientSecret(), clientSecret)) {throw new UnapprovedClientAuthenticationException("clientSecret不匹配" + clientId);}TokenRequest tokenRequest = new TokenRequest(MapUtils.EMPTY_MAP, clientId, clientDetails.getScope(), "custom");OAuth2Request oAuth2Request = tokenRequest.createOAuth2Request(clientDetails);OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(oAuth2Request, authentication);OAuth2AccessToken token = authorizationServerTokenServices.createAccessToken(oAuth2Authentication);response.setContentType("application/json;charset=UTF-8");response.getWriter().write(objectMapper.writeValueAsString(token));}else {super.onAuthenticationSuccess(request, response, authentication);}}
}
配置成功处理器
配置成功处理器在ebSecurityConfig里面配置。
/*** @author lvhaibao* @description 浏览器配置* @date 2018/12/25 0025 10:53*/
@Configuration
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate SecurityProperties securityProperties;@Autowiredprivate VcodeManager vcodeManager;@Autowiredprivate SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;@Autowiredprivate SpringSocialConfigurer mySocialSecurityConfig;@Autowiredprivate DataSource dataSource;@Autowiredprivate AuthenticationSuccessHandler myAuthenticationSuccessHandler;// @Override
// @Bean
// public AuthenticationManager authenticationManagerBean() throws Exception {
// return super.authenticationManagerBean();
// }/*** 生成记得我的token** @return*/@Beanpublic PersistentTokenRepository persistentTokenRepository() {//使用jdbc来存储JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();//设置数据源tokenRepository.setDataSource(dataSource);//当为true的时候就会自动创建表//tokenRepository.setCreateTableOnStartup(true);return tokenRepository;}@Overrideprotected void configure(HttpSecurity http) throws Exception {SmsCodeFilter smsCodeFilter = new SmsCodeFilter(vcodeManager);smsCodeFilter.setSecurityProperties(securityProperties);smsCodeFilter.afterPropertiesSet();http.addFilterBefore(smsCodeFilter, UsernamePasswordAuthenticationFilter.class)//表单登录,loginPage为登录请求的url,loginProcessingUrl为表单登录处理的URL.formLogin().loginPage(FromLoginConstant.LOGIN_PAGE).loginProcessingUrl(FromLoginConstant.LOGIN_PROCESSING_URL)//登录成功之后的处理.successHandler(myAuthenticationSuccessHandler)//允许访问.and().authorizeRequests().antMatchers(FromLoginConstant.LOGIN_PROCESSING_URL,FromLoginConstant.LOGIN_PAGE,securityProperties.getOauthLogin().getOauthLogin(),securityProperties.getOauthLogin().getOauthGrant(),"/myLogout","/code/sms")
// "/oauth/**").permitAll().anyRequest().authenticated()//禁用跨站伪造.and().csrf().disable()//短信验证码配置.apply(smsCodeAuthenticationSecurityConfig)//社交登录.and().apply(mySocialSecurityConfig);}}
手机号验证码登录
步骤
重写SmsCodeAuthenticationSecurityConfig
在SmsCodeAuthenticationSecurityConfig里面再配置成功处理器
/*** @author lvhaibao* @description* @date 2019/1/2 0002 10:39*/
@Component
public class SmsCodeAuthenticationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {@Autowiredprivate MyUserDetailsServiceImpl myUserDetailsService;@Autowiredprivate AuthenticationSuccessHandler myAuthenticationSuccessHandler;@Overridepublic void configure(HttpSecurity http) throws Exception {SmsCodeAuthenticationFilter smsCodeAuthenticationFilter = new SmsCodeAuthenticationFilter();//设置AuthenticationManagersmsCodeAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));//设置成功失败处理器smsCodeAuthenticationFilter.setAuthenticationSuccessHandler(myAuthenticationSuccessHandler);
// smsCodeAuthenticationFilter.setAuthenticationFailureHandler(myAuthenticationSuccessHandler);//设置providerSmsCodeAuthenticationProvider smsCodeAuthenticationProvider = new SmsCodeAuthenticationProvider();smsCodeAuthenticationProvider.setMyUserDetailsService(myUserDetailsService);http.authenticationProvider(smsCodeAuthenticationProvider).addFilterAfter(smsCodeAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);}
}
测试
用户名密码登录
使用工具。post请求如下地址:http://127.0.0.1/authentication/form。
手机号验证码登录
先获取验证码多少。get请求如下地址:
然后post请求如下地址:http://www.pinzhi365.com/authentication/mobile
项目源码
https://gitee.com/lvhaibao/spring-lhbauth/tree/34a4e781abeb9f1115b7a2e53141e0fed2915757/
spring oauth2 实现用户名密码登录、手机号验证码登录返回token相关推荐
- 测试案例:登录--手机号验证码登录
手机号验证码登录测试点 1.输入手机号码,待收到手机验证码后,输入验证码 ,成功 2.输入手机号码,待收到手机验证码后,输入错误的验证码,提示:失败 3.输入手机号码,待收到手机验证码后,更改原来手机 ...
- 微信小程序 账号密码 和 手机号 验证码 登录
微信小程序手机号登录页面 账号密码页面 手机号登录 账号密码页面 <!--pages/login/login.wxml--> <view class="container& ...
- 阿里云账户使用手机号+验证码登录开启方法、
阿里云账号登录支持手机号验证码登录方式,但是如果是2022.4.1前注册的账号,需要先在账号中心开启手机号登录,才可以使用手机号登录.阿里云百科来详细说下阿里云账号开启手机号登录的方法: 阿里云账号手 ...
- 手机号验证码登录的思路
引言 当前很多web端的应用登录方式主要分为以下几种: 账号密码登录 手机号验证码登录 扫码登录 这里我主要说一下我对于手机号验证码登录的思路,如果有遗漏或者差错的地方,请指正: 整体流程 大致流程如 ...
- Spring securty<六> 认证--手机号+验证码
Spring securty<六> 认证–手机号+验证码 文章目录 Spring securty<六> 认证--手机号+验证码 1.简介 2.认证(登录) 3.认证的流程 3. ...
- 小程序手机号验证码登录
这是我最近的一个项目--微信小程序 实现手机号- -验证码登录,涉及到了提交参数时需要加密.加密所采用的方式也是按照要求,严格采用16位小加密的MD5的加密形式. login.wxml <vie ...
- 前端Vue实现手机号验证码登录(60s禁用倒计时)
这篇文章主要介绍了Vue实现手机号验证码登录(60s禁用倒计时),帮助大家更好的理解和使用vue,具有很好的参考价值,感兴趣的朋友可以了解下 最近做了个vue项目,前端通过手机号验证码的方式登录,获取 ...
- 各种登录源码来了!基础登录、验证码登录、小程序登录...全都要!
现在开发个应用登录比以前麻烦的多.产品经理说用户名密码登录.短信登录都得弄上,如果搞个小程序连小程序登录也得安排上,差不多就是我全都要. 多种登录途径达到一个效果确实不太容易,今天胖哥在Spring ...
- Uni-app 手机号+验证码登录 用户名密码登录登出
用户名密码登录,手机号登录 用户名密码登录:用了uniapp+uview的$u.debounce防抖方法,再发送请求 判断正则以及同意条款勾选后,发起登录请求(参数是用户名和密码),如果请求成功--s ...
最新文章
- csgo怎么通过人机验证_让你的MacBook Pro完美运行CSGO!(图解版)
- 计算机软件评测减增值税,软件产品即征即退政策依据之一
- keyshot渲染图文教程_KeyShot渲染基础教程,教你五个步骤实现焦散线效果
- Angular form学习笔记
- 公共样式_设计干货 | 园路铺装的100种样式,保存收好
- flume记录(2)监控文件和目录,对hdfs操作
- netty心跳过程中 发送消息失败_Netty 4.0 实现心跳检测和断线重连
- Redis Sentinel 模拟故障迁移
- Vue + SpringBoot跨域
- 浙江大学PAT上机题解析之1008. 数组元素循环右移问题 (20)
- js ---- 对象去重
- 基于Windows Azure 搭建基于SharePoint 2010 Intranet、Extranet、Internet (3): 配置负载均衡...
- Flutter之RenderView RenderObject ParentData知识点梳理
- PDF怎么把两个合并成一个?PDF怎么合并?
- 浅谈Java垃圾回收
- 手机电视(CMMB+MBBMS)安全架构原理
- fastposter v2.7.0 发布 电商海报编辑器
- 利用Python实现FGO自动战斗脚本,再也不用爆肝啦~
- 教育孩子,是从小的润雨细无声。纯属个人文学闷骚型。。。
- *6-1 CCF 2015-03-2 数字排序
热门文章
- java insert row,POI ,Java 操作 Excel 实现行的插入(insert row)
- 如何使用Fiddler模拟弱网情况对app进行测试
- html让页面整体可以上下移动,html5 touch事件实现触屏页面上下滑动(二)
- 更新微软Edge后,发现NewBing边栏快捷插件没有了,新bing侧边栏,侧边栏快捷键
- QPixmap: It is not safe to use pixmaps outside the GUI thread
- CTEX之WinEdt编辑器相关设置
- 离散数学(荣誉) 作业一
- STEMA (STEM 能力测试) 考 试 白 皮 书--scratch编程部分例子试题及实现
- 洁净工程施工建设洁净室工程
- MySQL替换回车符\n