Spring OAuth2 GitHub 自定义登录信息
# 相关代码
https://github.com/mofadeyunduo/money
0.1.3-SNAPSHOT
security 模块中
# 原因
最近在做一款管理金钱的网站进行自娱自乐,发现没有安全控制岂不是大家都知道我的工资了(一脸黑线)?
最近公司也在搞 Spring OAuth2,当时我没有时间(其实那时候不想搞)就没做,现在回头来学习学习。
Spring OAuth2 官方的教程写的比较少,实用性比较差。
# 教程内容
- Spring OAuth2 Github SSO
- 替换认证过的 Spring Security Authentication 对象
- 后端设置 Cookie 实现 Remember-Me 功能
# Github 登录
Spring OAuth2 Github,官网写的已经比较详细了。
要注意的是必须要配置两个 Filter:
- OAuth2ClientAuthenticationProcessingFilter ,用于处理 OAuth2 流程。
- OAuth2ClientContextFilter,用于触发跳转到 OAuth2 请求。
P.S. 这很诡异,我不知道 Spring OAuth2 为什么要这么做。
# 替换认证过的 Spring Security Authentication
步骤:
- 重写方法 getPrincipal,把 Authentication 中的 Principal 换成用户信息对象。
- 最好设置 AuthenticationSuccessHandler,默认的 AuthenticationSuccessHandler 会跳转到主页,并将 Cookie 清空,影响 RememberMeServices 认证。
@Override
protected Object getPrincipal(Map<String, Object> map) {
String principal = String.class.cast(map.get("name"));
// 从数据库读取用户
UserWithAccount userWithAccount = userAndAccountService.getByPrincipalAndType(principal, AccountType.GITHUB);
// 未获取到用户信息,保存
if (Objects.isNull(userWithAccount)) {
Account newAccount = new Account(null, AccountType.GITHUB, principal, new Gson().toJson(map));
userWithAccount = userAndAccountService.accountSignup(newAccount);
}
// 替换原来的 OAuth2Authentication
return userWithAccount;
}
githubFilter.setAuthenticationSuccessHandler(new GithubAuthenticationSuccessHandler());
# 设置 Cookie
- 实现 UserDetailServices。
- 用 UserDetailServices 构造 RememberMeServices。这里采用的 RememberMeServices 的具体实现是 TokenBasedRememberMeServices。TokenBasedRememberMeServices 会用 UserDetailServices 构造 Authentication 的 Principal 对象。还有一个构造参数(key)是指定盐,指定一个值,该值在应用反复重启的时要保持不变。顺便提一下,TokenBasedRememberMeServices 加密是根据 UserDetailServices 构造出的 UserDetails 中的 username、password、时间戳、构造参数 key 进行 MD5 和 Base64 加密和解密。
- 在 OAuth2ClientAuthenticationProcessingFilter 和继承类 WebSecurityConfigurerAdapter 的方法 configure 注册 RememberMeServices。注册到 OAuth2ClientAuthenticationProcessingFilter 是为了在 OAuth2 认证成功或者失败之后设置 Cookie。在 configure 注册 RememberMeServices 是为了利用 Cookie 自动登录。
public class CachingUserDetailsService implements UserDetailsService {
private static final String USER_KEY = "user:%s";
private StringRedisTemplate stringRedisTemplate;
private UserAndAccountService userAndAccountService;
public CachingUserDetailsService(StringRedisTemplate stringRedisTemplate, UserAndAccountService userAndAccountService) {
this.stringRedisTemplate = stringRedisTemplate;
this.userAndAccountService = userAndAccountService;
}
public UserDetails loadUserByUsername(String username) {
String actualKey = String.format(USER_KEY, username);
UserWithAccount userWithAccount;
String userWithAccountJson = stringRedisTemplate.opsForValue().get(actualKey);
if (StringUtils.isEmpty(userWithAccountJson)) {
userWithAccount = userAndAccountService.getByUserId(Integer.parseInt(username));
stringRedisTemplate.opsForValue().set(actualKey, new Gson().toJson(userWithAccount));
} else {
userWithAccount = new Gson().fromJson(userWithAccountJson, UserWithAccount.class);
}
return userWithAccount;
}
}
@Bean
public RememberMeServices rememberMeServices() {
TokenBasedRememberMeServices tokenBasedRememberMeServices = new TokenBasedRememberMeServices(REMEMBER_ME_KEY, userDetailsService());
tokenBasedRememberMeServices.setAlwaysRemember(true);
tokenBasedRememberMeServices.setTokenValiditySeconds(ONE_DAY_IN_SECONDS);
return tokenBasedRememberMeServices;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.rememberMe()
.key(REMEMBER_ME_KEY)
.rememberMeServices(rememberMeServices());
http
.addFilterBefore(oAuth2ClientAuthenticationProcessingFilter(), BasicAuthenticationFilter.class); // Github OAuth2 登录的 Filter
} @Bean
public OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationProcessingFilter() {
OAuth2ClientAuthenticationProcessingFilter githubFilter = new OAuth2ClientAuthenticationProcessingFilter("/security/oauth2/github");
OAuth2RestTemplate facebookTemplate = new OAuth2RestTemplate(githubClient(), oauth2ClientContext);
githubFilter.setAllowSessionCreation(false);
githubFilter.setRestTemplate(facebookTemplate);
githubFilter.setTokenServices(new GithubUserInfoTokenServices(githubResource().getUserInfoUri(), githubClient().getClientId(), userAndAccountService));
githubFilter.setRememberMeServices(rememberMeServices());
githubFilter.setAuthenticationSuccessHandler(new GithubAuthenticationSuccessHandler());
return githubFilter;
}
# 后记
我本来预估一周时间就把我项目的安全控制模块给开发完,结果被 Spring Oauth2 拖了一周。由于对 Spring 核心概念也不是特别熟悉,看代码也比较费事(虽然大部分看得懂)。最近一段时间准备写一套 IoC 方面的代码。
我现在所在的公司之前有个人挺厉害,自己写了一套 IoC 容器用到了系统中。不过他也很坑,他写的 IoC 容器出了问题,根本找不到任何解决资料,只能看源码。自己写的只能做玩具玩玩吧。
Spring OAuth2 GitHub 自定义登录信息的更多相关文章
- DedeCMS中实现在顶层banner中显示自定义登录信息
一.需求描述 dedeCMS自带的模板中有互动中心模块,如下图所示: 由于会员登陆对我来说不是网站的重要模块且默认DedeCMS的会员中心模块的初始化很慢,常会显示“正在载入中,请稍候...”, 所以 ...
- spring oauth2获取当前登录用户信息。
使用spring oauth2框架做授权鉴定.想获取当前用户信息怎么办? 我们知道spring oauth2是基于spring security的实现的. spring security可以通过Sec ...
- spring security使用自定义登录界面后,不能返回到之前的请求界面的问题
昨天因为集成spring security oauth2,所以对之前spring security的配置进行了一些修改,然后就导致登录后不能正确跳转回被拦截的页面,而是返回到localhost根目录. ...
- spring security采用自定义登录页和退出功能
更新... 首先采用的是XML配置方式,请先查看 初识Spring security-添加security 在之前的示例中进行代码修改 项目结构如下: 一.修改spring-security.xml ...
- Spring Security 自定义登录页面
SpringMVC + Spring Security,自定义登录页面登录验证 学习参考:http://www.mkyong.com/spring-security/spring-security-f ...
- SpringCloud微服务实战——搭建企业级开发框架(四十):使用Spring Security OAuth2实现单点登录(SSO)系统
一.单点登录SSO介绍 目前每家企业或者平台都存在不止一套系统,由于历史原因每套系统采购于不同厂商,所以系统间都是相互独立的,都有自己的用户鉴权认证体系,当用户进行登录系统时,不得不记住每套系统的 ...
- 【SpringSecurityOAuth2】源码分析@EnableOAuth2Sso在Spring Security OAuth2 SSO单点登录场景下的作用
目录 一.从Spring Security OAuth2官方文档了解@EnableOAuth2Sso作用 二.源码分析@EnableOAuth2Sso作用 @EnableOAuth2Client OA ...
- Spring Security笔记:自定义登录页
以下内容参考了 http://www.mkyong.com/spring-security/spring-security-form-login-example/ 接上回,在前面的Hello Worl ...
- Spring Security入门(2-3)Spring Security 的运行原理 4 - 自定义登录方法和页面
参考链接,多谢作者: http://blog.csdn.net/lee353086/article/details/52586916 http元素下的form-login元素是用来定义表单登录信息的. ...
随机推荐
- #pragma预处理命令
#pragma comment(lib,"XXX.lib") 表示链接XXX.lib这个库,和在工程设置里写上XXX.lib的效果一样. #pragma comment(linke ...
- python中__name__=='__main__'的作用
学习python语法的过程中碰到了__name__=='__main__',这里做个笔记. 作用 这段代码的作用就是让你写的脚本模块既可以导入到别的模块中用,另外该模块自己也可执行. 测试 先 ...
- C. Polycarp at the Radio
这题题意不太好理解,但是可以通过样例推.主要考察思维的全面性,注意把b[m]特殊处理下. AC代码: #include<cstdio> #include<cstring> co ...
- Linux CentOS安装配置MySQL数据库
没什么好说的,直接正面刚吧. 安装mysql数据库 a)下载mysql源安装包:wget http://dev.mysql.com/get/mysql57-community-release-el7- ...
- caffe+GAN︱PPGN生成模型5则官方案例(caffe版)
一.效果与架构 PPGN 整合了对抗训练.cnn特征匹配.降噪自编码.Langevin采样:在NIPS2016得到了Ian Goodfellow的介绍. PPGN生成的图像同类差异化大,可根据指定生成 ...
- mysql数据库相关基本术语和概念
1.DDL:Data Definition Language,即数据定义语言,定义数据库涉及的各种对象,定义数据的完整性约束.保密限制等约束. 2.DML:Data Manipulation Lang ...
- Activiti 实战篇 小试牛刀
原文地址:http://blog.csdn.net/qq_30739519/article/details/51166062 1:工作流的概念 说明: 1) 假设:这两张图就是华谊兄弟的请假流程图 ...
- HighCharts之2D柱状图、折线图的组合双轴图
HighCharts之2D柱状图.折线图的组合双轴图 1.实例源码 DoubleAxis.html: <!DOCTYPE html> <html> <head> & ...
- Jquery ajaxfileupload.js结合.ashx文件实现无刷新上传
先上几张图更直观展示一下要实现的功能,本功能主要通过Jquery ajaxfileupload.js插件结合ajaxUpFile.ashx一般应用程序处理文件实现Ajax无刷新上传功能,结合NPOI2 ...
- Caused by:org.hibernate.HibernateException:Unable to make JDBC Connection
1.错误描述 Caused by:org.hibernate.HibernateException:Unable to make JDBC Connection[jdbc\:mysql\://loca ...