boot-starter-security-ouath模块完善oauth2服务提供商功能

This commit is contained in:
winLsy
2018-03-17 13:06:54 +08:00
parent 61bfc473ae
commit 164302e1be
8 changed files with 101 additions and 6 deletions
@@ -22,6 +22,8 @@
- oauth2中相同的用户会得到相同的token
- `ms.security.oauth2.storeType`token的策略,不设置这个值的是时候默认生成`jwt`的token,如果设置值为`redis`的时候生成的默认的token将会存储在redis中
- `ms.security.oauth2.jwtSigningKey` 生成jwt的token的秘钥
- `ms.security.oauth2.providerLoginPage`oauth2服务提供商提供用户登录的页面
- `ms.security.oauth2.providerLoginProcessUrl`oauth2服务提供商处理用户登录进程的url
- `ms.security.oauth2.clients[i].clientId` 配置oauth2授权的clientId
- `ms.security.oauth2.clients[i].clientSecret` 配置oauth2授权的clientId对应的clientSecret
- `ms.security.oauth2.clients[i].accessTokenValiditySeconds` 数组token过期时间,单位是秒
@@ -15,7 +15,9 @@ import org.springframework.security.oauth2.config.annotation.web.configuration.R
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
/**
* OAuth2协议标准服务提供实现配置类,资源服务器的配置。用于受信任的自家的应用和进行密码模式的直接登录
* OAuth2协议标准服务提供实现配置类,资源服务器的配置,其中服用了oauth服务商签发令牌的流程。
* 用于受信任的自家的应用进行多种模式(图片验证码,手机短信验证,社交登录)的登录。
* 这个过滤器链的优先级低于oauth2服务商中httpSecurity的优先级
* @author lsy
*/
@Configuration
@@ -72,7 +74,10 @@ public class AuthorizationResourceConfig extends ResourceServerConfigurerAdapter
, SecurityConstants.FORM_LOGIN_PROCESSOR_URI
, SecurityConstants.GET_VALIDATE_CODE_URI
, SecurityConstants.LOGIN_PAGE
// 开发对数据库连接池的访问权限
, "/druid/**"
// 开发oauth提供商的权限
, "/oauth/**"
)
.permitAll()
//任何请求
@@ -1,4 +1,4 @@
package com.micro.fast.security.app.config.authorization2;
package com.micro.fast.security.app.config.authorization2.provider;
import com.micro.fast.security.app.master.OAuth2ClientProperties;
import com.micro.fast.security.app.master.SecurityProperties;
@@ -21,6 +21,12 @@ import java.util.ArrayList;
import java.util.List;
/**
* /oauth/authorize 第三方点击进行开始授权的url。这个地址应该是受spring-security保护的,只有登录的用户才能访问
* /oauth/confirm_access 确认授权
* /oauth/token 第三方获取token的地址
* /oauth/error 授权发生错误时默认的处理地址
* /oauth/check_token 解码校验token的地址
* /oauth/token_key 使用jwtToken时暴露publicKey的地址
* OAuth2协议标准服务商实现配置类,配置token的生成存储策略。用于第三方的应用进行登录
* @author lsy
*/
@@ -38,6 +44,7 @@ public class AuthorizationProviderConfig extends AuthorizationServerConfigurerAd
private SecurityProperties securityProperties;
/**
* jwtToken转换器
* 只有在jwt环境下才生效
*/
@Autowired(required = false)
@@ -50,7 +57,7 @@ public class AuthorizationProviderConfig extends AuthorizationServerConfigurerAd
private TokenStore tokenStore;
/**
* 配置token的存取策略,以及增强策略
* 配置token的形式,存取策略,以及增强策略
* @param endpoints
* @throws Exception
*/
@@ -70,12 +77,15 @@ public class AuthorizationProviderConfig extends AuthorizationServerConfigurerAd
enhancerChain.setTokenEnhancers(enhancers);
//将增强链放入扩展点中
endpoints.tokenEnhancer(enhancerChain)
// 用于解析jwtToken
.accessTokenConverter(jwtAccessTokenConverter);
}
}
/**
* 配置哪些应用可以获取token
* 可以配置在内存中存贮,也可以配置在数据库中存储.
* 存储在数据中的时候可以使用 JdbcClientDetailsService对信息进行动态修改
* @param clients
* @throws Exception
*/
@@ -0,0 +1,56 @@
package com.micro.fast.security.app.config.authorization2.provider;
import com.micro.fast.security.app.master.SecurityProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
/**
* oauth2服务提供商的授权拦截的配置,用于拦截/oauth/authorize过滤器链的优先级高于资源服务器授权过滤连的优先级
* @author lsy
*/
@EnableWebSecurity
public class Oauth2ProviderSecurity extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationSuccessHandler successHandler;
@Autowired
private AuthenticationFailureHandler failureHandler;
@Autowired
private SecurityProperties securityProperties;
/**
* 安全配置
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http // 表单登录的配置
.formLogin()
// 自定义的登录页面地址
.loginPage(securityProperties.getOauth2().getProviderLoginPage())
// 处理登录进程的url
.loginProcessingUrl(securityProperties.getOauth2().getProviderLoginProcessUrl())
// 登录成功处理器
.successHandler(successHandler)
// 登录失败处理器
.failureHandler(failureHandler)
.and()
.authorizeRequests()
// 任何请求都允许不进行授权
.anyRequest()
.permitAll()
// 符合这些规则的需要进行授权,对授权登录接口进行授权
.antMatchers("/oauth/authorize")
.authenticated()
.and()
.csrf()
.disable();
}
}
@@ -20,7 +20,7 @@ import java.io.IOException;
*/
@Slf4j
@RestController
public class SecurityController {
public class ResourceSecurityController {
/**
* 用于获取引发身份认证的请求缓存
@@ -28,7 +28,7 @@ public class SecurityController {
private RequestCache requestCache = new HttpSessionRequestCache();
/**
* 告知前台,需要进行登录
* 告知前台,需要进行登录。
* @param request
* @param response
* @return
@@ -23,6 +23,10 @@ public class TokenStoreConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
/**
* 存入redis中
* @return
*/
@Bean
@ConditionalOnProperty(prefix = "ms.security.oauth2",name = "storeType",havingValue = "redis",matchIfMissing = false)
public TokenStore redisTokenStore(){
@@ -11,9 +11,28 @@ import lombok.Setter;
@Setter
public class OAuth2Properties {
/**
* oauth2客户端详情
*/
private OAuth2ClientProperties[] clients = new OAuth2ClientProperties[]{};
/**
* jwt签名所需要的秘钥
*/
private String jwtSigningKey = "msjwt";
/**
* 登录认证token的方式
*/
private String storeType = "jwt";
/**
* 服务提供商授权时用户的登录界面
*/
private String providerLoginPage = "/oauth/login";
/**
* 服务提供商授权时处理登录的路径
*/
private String providerLoginProcessUrl = "/oauth/login/process";
}
-1
View File
@@ -19,7 +19,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR3</spring-cloud.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>