增加OIDC协议支持
This commit is contained in:
@@ -1,22 +1,38 @@
|
||||
package com.central.oauth.config;
|
||||
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.model.SysUser;
|
||||
import com.central.oauth.model.Client;
|
||||
import com.central.oauth.service.IClientService;
|
||||
import com.central.oauth.service.impl.RedisClientDetailsService;
|
||||
import com.central.oauth.utils.OidcIdTokenBuilder;
|
||||
import com.central.oauth2.common.constants.IdTokenClaimNames;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.cloud.bootstrap.encrypt.KeyProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
||||
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
|
||||
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.oauth2.provider.TokenGranter;
|
||||
import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices;
|
||||
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
|
||||
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
|
||||
import org.springframework.security.oauth2.provider.token.TokenStore;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* OAuth2 授权服务器配置
|
||||
@@ -55,6 +71,9 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
|
||||
@Autowired
|
||||
private TokenGranter tokenGranter;
|
||||
|
||||
@Value("${zlt.oauth2.token.store.type:'redis'}")
|
||||
private String tokenStoreType;
|
||||
|
||||
/**
|
||||
* 配置身份认证器,配置认证方式,TokenStore,TokenGranter,OAuth2RequestFactory
|
||||
* @param endpoints
|
||||
@@ -93,4 +112,59 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
|
||||
//让/oauth/token支持client_id以及client_secret作登录认证
|
||||
.allowFormAuthenticationForClients();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(1)
|
||||
public TokenEnhancer tokenEnhancer(@Autowired(required = false) KeyProperties keyProperties
|
||||
, IClientService clientService) {
|
||||
return (accessToken, authentication) -> {
|
||||
Set<String> responseTypes = authentication.getOAuth2Request().getResponseTypes();
|
||||
if (responseTypes.contains(SecurityConstants.ID_TOKEN)
|
||||
|| "authJwt".equals(tokenStoreType)) {
|
||||
Map<String, Object> additionalInfo = new HashMap<>(2);
|
||||
Object principal = authentication.getPrincipal();
|
||||
//增加id参数
|
||||
if (principal instanceof SysUser) {
|
||||
SysUser user = (SysUser)principal;
|
||||
if (responseTypes.contains(SecurityConstants.ID_TOKEN)) {
|
||||
//生成id_token
|
||||
setIdToken(additionalInfo, authentication, keyProperties, clientService, user);
|
||||
}
|
||||
if ("authJwt".equals(tokenStoreType)) {
|
||||
additionalInfo.put("id", user.getId());
|
||||
}
|
||||
}
|
||||
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
|
||||
}
|
||||
return accessToken;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成id_token
|
||||
* @param additionalInfo 存储token附加信息对象
|
||||
* @param authentication 授权对象
|
||||
* @param keyProperties 密钥
|
||||
* @param clientService 应用service
|
||||
*/
|
||||
private void setIdToken(Map<String, Object> additionalInfo, OAuth2Authentication authentication
|
||||
, KeyProperties keyProperties, IClientService clientService, SysUser user) {
|
||||
String clientId = authentication.getOAuth2Request().getClientId();
|
||||
Client client = clientService.loadClientByClientId(clientId);
|
||||
if (client.getSupportIdToken()) {
|
||||
String nonce = authentication.getOAuth2Request().getRequestParameters().get(IdTokenClaimNames.NONCE);
|
||||
long now = System.currentTimeMillis();
|
||||
long expiresAt = System.currentTimeMillis() + client.getIdTokenValiditySeconds() * 1000;
|
||||
String idToken = OidcIdTokenBuilder.builder(keyProperties)
|
||||
.issuer(SecurityConstants.ISS)
|
||||
.issuedAt(now)
|
||||
.expiresAt(expiresAt)
|
||||
.subject(String.valueOf(user.getId()))
|
||||
.audience(clientId)
|
||||
.nonce(nonce)
|
||||
.build();
|
||||
|
||||
additionalInfo.put(SecurityConstants.ID_TOKEN, idToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,4 +32,13 @@ public class Client extends SuperEntity {
|
||||
private Integer refreshTokenValiditySeconds = 28800;
|
||||
private String additionalInformation = "{}";
|
||||
private String autoapprove = "true";
|
||||
/**
|
||||
* 是否支持id_token
|
||||
*/
|
||||
private Boolean supportIdToken = true;
|
||||
/**
|
||||
* id_token有效时间(s)
|
||||
*/
|
||||
@TableField(value = "id_token_validity")
|
||||
private Integer idTokenValiditySeconds = 60;
|
||||
}
|
||||
|
||||
@@ -24,4 +24,6 @@ public interface IClientService extends ISuperService<Client> {
|
||||
PageResult<Client> listClient(Map<String, Object> params, boolean isPage);
|
||||
|
||||
void delClient(long id);
|
||||
|
||||
Client loadClientByClientId(String clientId);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.central.oauth.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.lock.DistributedLock;
|
||||
import com.central.common.redis.template.RedisRepository;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
@@ -72,6 +72,13 @@ public class ClientServiceImpl extends SuperServiceImpl<ClientMapper, Client> im
|
||||
redisRepository.del(clientRedisKey(clientId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Client loadClientByClientId(String clientId) {
|
||||
QueryWrapper<Client> wrapper = Wrappers.query();
|
||||
wrapper.eq("client_id", clientId);
|
||||
return this.getOne(wrapper);
|
||||
}
|
||||
|
||||
private String clientRedisKey(String clientId) {
|
||||
return SecurityConstants.CACHE_CLIENT_KEY + ":" + clientId;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
package com.central.oauth.utils;
|
||||
|
||||
import com.central.common.utils.JsonUtil;
|
||||
import com.central.oauth2.common.util.JwtUtils;
|
||||
import lombok.Getter;
|
||||
import org.springframework.cloud.bootstrap.encrypt.KeyProperties;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static com.central.oauth2.common.constants.IdTokenClaimNames.*;
|
||||
|
||||
/**
|
||||
* Oidc协议的IdToken
|
||||
*
|
||||
* @author zlt
|
||||
* @version 1.0
|
||||
* @date 2021/4/21
|
||||
* <p>
|
||||
* Blog: https://zlt2000.gitee.io
|
||||
* Github: https://github.com/zlt2000
|
||||
*/
|
||||
@Getter
|
||||
public class OidcIdTokenBuilder {
|
||||
private final Map<String, Object> claims;
|
||||
private final KeyProperties keyProperties;
|
||||
|
||||
private OidcIdTokenBuilder(KeyProperties keyProperties) {
|
||||
this.claims = new LinkedHashMap<>();
|
||||
this.keyProperties = keyProperties;
|
||||
}
|
||||
|
||||
public static OidcIdTokenBuilder builder(KeyProperties keyProperties) {
|
||||
Assert.notNull(keyProperties, "keyProperties required");
|
||||
return new OidcIdTokenBuilder(keyProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this claim in the resulting {@link OidcIdTokenBuilder}
|
||||
*
|
||||
* @param name The claim name
|
||||
* @param value The claim value
|
||||
*/
|
||||
public OidcIdTokenBuilder claim(String name, Object value) {
|
||||
this.claims.put(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this audience in the resulting {@link OidcIdTokenBuilder}
|
||||
*
|
||||
* @param audience The audience to use
|
||||
*/
|
||||
public OidcIdTokenBuilder audience(String audience) {
|
||||
return claim(AUD, audience);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this expiration in the resulting {@link OidcIdTokenBuilder}
|
||||
*
|
||||
* @param expiresAt The expiration to use
|
||||
*/
|
||||
public OidcIdTokenBuilder expiresAt(long expiresAt) {
|
||||
return this.claim(EXP, expiresAt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this issued-at timestamp in the resulting {@link OidcIdTokenBuilder}
|
||||
*
|
||||
* @param issuedAt The issued-at timestamp to use
|
||||
*/
|
||||
public OidcIdTokenBuilder issuedAt(long issuedAt) {
|
||||
return this.claim(IAT, issuedAt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this issuer in the resulting {@link OidcIdTokenBuilder}
|
||||
*
|
||||
* @param issuer The issuer to use
|
||||
*/
|
||||
public OidcIdTokenBuilder issuer(String issuer) {
|
||||
return this.claim(ISS, issuer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this nonce in the resulting {@link OidcIdTokenBuilder}
|
||||
*
|
||||
* @param nonce The nonce to use
|
||||
*/
|
||||
public OidcIdTokenBuilder nonce(String nonce) {
|
||||
return this.claim(NONCE, nonce);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this subject in the resulting {@link OidcIdTokenBuilder}
|
||||
*
|
||||
* @param subject The subject to use
|
||||
*/
|
||||
public OidcIdTokenBuilder subject(String subject) {
|
||||
return this.claim(SUB, subject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the {@link OidcIdTokenBuilder}
|
||||
*
|
||||
* @return The constructed {@link OidcIdTokenBuilder}
|
||||
*/
|
||||
public String build() {
|
||||
return JwtUtils.encodeStr(JsonUtil.toJSONString(claims), keyProperties);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user