# 相关代码

https://github.com/mofadeyunduo/money

0.1.3-SNAPSHOT

security 模块中

# 原因

最近在做一款管理金钱的网站进行自娱自乐,发现没有安全控制岂不是大家都知道我的工资了(一脸黑线)?

最近公司也在搞 Spring OAuth2,当时我没有时间(其实那时候不想搞)就没做,现在回头来学习学习。

Spring OAuth2 官方的教程写的比较少,实用性比较差。

# 教程内容

  1. Spring OAuth2 Github SSO
  2. 替换认证过的 Spring Security Authentication 对象
  3. 后端设置 Cookie 实现 Remember-Me 功能

# Github 登录

Spring OAuth2 Github,官网写的已经比较详细了。

要注意的是必须要配置两个 Filter:

  • OAuth2ClientAuthenticationProcessingFilter ,用于处理 OAuth2 流程。
  • OAuth2ClientContextFilter,用于触发跳转到 OAuth2 请求。

P.S. 这很诡异,我不知道 Spring OAuth2 为什么要这么做。

# 替换认证过的 Spring Security Authentication

步骤:

  1. 重写方法 getPrincipal,把 Authentication 中的 Principal 换成用户信息对象。
  2. 最好设置 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

  1. 实现 UserDetailServices。
  2. 用 UserDetailServices 构造 RememberMeServices。这里采用的 RememberMeServices 的具体实现是 TokenBasedRememberMeServices。TokenBasedRememberMeServices 会用 UserDetailServices 构造 Authentication 的 Principal 对象。还有一个构造参数(key)是指定盐,指定一个值,该值在应用反复重启的时要保持不变。顺便提一下,TokenBasedRememberMeServices 加密是根据 UserDetailServices 构造出的 UserDetails 中的 username、password、时间戳、构造参数 key 进行 MD5 和 Base64 加密和解密。
  3. 在 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 自定义登录信息的更多相关文章

  1. DedeCMS中实现在顶层banner中显示自定义登录信息

    一.需求描述 dedeCMS自带的模板中有互动中心模块,如下图所示: 由于会员登陆对我来说不是网站的重要模块且默认DedeCMS的会员中心模块的初始化很慢,常会显示“正在载入中,请稍候...”, 所以 ...

  2. spring oauth2获取当前登录用户信息。

    使用spring oauth2框架做授权鉴定.想获取当前用户信息怎么办? 我们知道spring oauth2是基于spring security的实现的. spring security可以通过Sec ...

  3. spring security使用自定义登录界面后,不能返回到之前的请求界面的问题

    昨天因为集成spring security oauth2,所以对之前spring security的配置进行了一些修改,然后就导致登录后不能正确跳转回被拦截的页面,而是返回到localhost根目录. ...

  4. spring security采用自定义登录页和退出功能

    更新... 首先采用的是XML配置方式,请先查看  初识Spring security-添加security 在之前的示例中进行代码修改 项目结构如下: 一.修改spring-security.xml ...

  5. Spring Security 自定义登录页面

    SpringMVC + Spring Security,自定义登录页面登录验证 学习参考:http://www.mkyong.com/spring-security/spring-security-f ...

  6. SpringCloud微服务实战——搭建企业级开发框架(四十):使用Spring Security OAuth2实现单点登录(SSO)系统

    一.单点登录SSO介绍   目前每家企业或者平台都存在不止一套系统,由于历史原因每套系统采购于不同厂商,所以系统间都是相互独立的,都有自己的用户鉴权认证体系,当用户进行登录系统时,不得不记住每套系统的 ...

  7. 【SpringSecurityOAuth2】源码分析@EnableOAuth2Sso在Spring Security OAuth2 SSO单点登录场景下的作用

    目录 一.从Spring Security OAuth2官方文档了解@EnableOAuth2Sso作用 二.源码分析@EnableOAuth2Sso作用 @EnableOAuth2Client OA ...

  8. Spring Security笔记:自定义登录页

    以下内容参考了 http://www.mkyong.com/spring-security/spring-security-form-login-example/ 接上回,在前面的Hello Worl ...

  9. Spring Security入门(2-3)Spring Security 的运行原理 4 - 自定义登录方法和页面

    参考链接,多谢作者: http://blog.csdn.net/lee353086/article/details/52586916 http元素下的form-login元素是用来定义表单登录信息的. ...

随机推荐

  1. UVA - 10285 Longest Run on a Snowboard (线性DP)

    思路:d[x][y]表示以(x, y)作为起点能得到的最长递减序列,转移方程d[x][y] = max(d[px][py] + 1),此处(px, py)是它的相邻位置并且该位置的值小于(x, y)处 ...

  2. django-装饰器实现PV统计

    1.models层建立统计表 # 每日访问量统计 class Statistics(models.Model): pv = models.IntegerField(default=0) uv = mo ...

  3. 【前端】HTML中最适合做按钮的元素

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/6405914.html 可选的可以做按钮的元素有如下几个 a.input.button.div(span等) 场景 ...

  4. caffe︱ImageData层、DummyData层作为原始数据导入的应用

    Part1:caffe的ImageData层 ImageData是一个图像输入层,该层的好处是,直接输入原始图像信息就可以导入分析. 在案例中利用ImageData层进行数据转化,得到了一批数据. 但 ...

  5. jvm类加载器和双亲委派模型

    类加载器按照层次,从顶层到底层,分为以下三种:  (1)启动类加载器(Bootstrap ClassLoader)   这个类加载器负责将存放在JAVA_HOME/lib下的,或者被-Xbootcla ...

  6. Ubuntu 卸载cario-dock

    偶然间听说别人用dock 可以把ubuntu美化,结果就装了个cairo-dock .结果是苹果mac的风格.不是很喜欢.于是就卸载,卸载过程中.发行卸载不掉. 尝试了很多方法. sudo apt-g ...

  7. Java中字符串的一些常见方法

    1.Java中字符串的一些常见方法 /** * */ package com.you.model; /** * @author Administrator * @date 2014-02-24 */ ...

  8. CAN总线基础知识(三)

    1.CAN协议 1.1 帧类型 通讯时使用下面5个类型的帧: 数据帧 遥控帧 错误帧 过载帧 帧间空隙 在所有这些帧中,数据帧和遥控帧由用户设置,而其它帧则由CAN硬件设置. 数据和遥控帧有两种格式: ...

  9. Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be ope

    1.错误描述 java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.tes ...

  10. freemarker写select组件(一)

    freemarker写select组件 1.宏定义 <#macro select id datas> <select id="${id}" name=" ...