优化OIDC协议

This commit is contained in:
zlt2000
2021-05-22 15:39:24 +08:00
parent 7431a35e9c
commit 6d50fc995a
9 changed files with 132 additions and 34 deletions
@@ -7,8 +7,8 @@ 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 com.central.oauth2.common.properties.TokenStoreProperties;
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;
@@ -71,9 +71,6 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
@Autowired
private TokenGranter tokenGranter;
@Value("${zlt.oauth2.token.store.type:'redis'}")
private String tokenStoreType;
/**
* 配置身份认证器,配置认证方式,TokenStore,TokenGranter,OAuth2RequestFactory
* @param endpoints
@@ -116,11 +113,12 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
@Bean
@Order(1)
public TokenEnhancer tokenEnhancer(@Autowired(required = false) KeyProperties keyProperties
, IClientService clientService) {
, IClientService clientService
, TokenStoreProperties tokenStoreProperties) {
return (accessToken, authentication) -> {
Set<String> responseTypes = authentication.getOAuth2Request().getResponseTypes();
if (responseTypes.contains(SecurityConstants.ID_TOKEN)
|| "authJwt".equals(tokenStoreType)) {
|| "authJwt".equals(tokenStoreProperties.getType())) {
Map<String, Object> additionalInfo = new HashMap<>(2);
Object principal = authentication.getPrincipal();
//增加id参数
@@ -130,7 +128,7 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
//生成id_token
setIdToken(additionalInfo, authentication, keyProperties, clientService, user);
}
if ("authJwt".equals(tokenStoreType)) {
if ("authJwt".equals(tokenStoreProperties.getType())) {
additionalInfo.put("id", user.getId());
}
}
@@ -160,6 +158,9 @@ public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdap
.issuedAt(now)
.expiresAt(expiresAt)
.subject(String.valueOf(user.getId()))
.name(user.getNickname())
.loginName(user.getUsername())
.picture(user.getHeadImgUrl())
.audience(clientId)
.nonce(nonce)
.build();
@@ -1,15 +1,28 @@
package com.central.oauth.controller;
import com.central.common.annotation.LoginClient;
import com.central.common.constant.SecurityConstants;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.oauth.model.TokenVo;
import com.central.oauth.service.ITokensService;
import com.central.oauth2.common.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.stream.Collectors;
/**
* token管理接口
@@ -17,15 +30,39 @@ import java.util.Map;
* @author zlt
*/
@Api(tags = "Token管理")
@Slf4j
@RestController
@RequestMapping("/tokens")
public class TokensController {
@Autowired
@Resource
private ITokensService tokensService;
@Resource
private ClientDetailsService clientDetailsService;
@Resource
private PasswordEncoder passwordEncoder;
@GetMapping("")
@ApiOperation(value = "token列表")
public PageResult<TokenVo> list(@RequestParam Map<String, Object> params, String tenantId) {
return tokensService.listTokens(params, tenantId);
}
@GetMapping("/key")
@ApiOperation(value = "获取jwt密钥")
public Result<String> key(HttpServletRequest request) {
String[] clientArr = AuthUtils.extractClient(request);
ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientArr[0]);
if (!passwordEncoder.matches(clientArr[1], clientDetails.getClientSecret())) {
throw new BadCredentialsException("应用密码错误");
}
org.springframework.core.io.Resource res = new ClassPathResource(SecurityConstants.RSA_PUBLIC_KEY);
try (BufferedReader br = new BufferedReader(new InputStreamReader(res.getInputStream()))) {
return Result.succeed(br.lines().collect(Collectors.joining("\n")));
} catch (IOException ioe) {
log.error("key error", ioe);
return Result.failed(ioe.getMessage());
}
}
}
@@ -1,5 +1,6 @@
package com.central.oauth.service.impl;
import com.central.common.constant.SecurityConstants;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.common.*;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
@@ -9,6 +10,7 @@ import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.Set;
import java.util.UUID;
/**
@@ -55,6 +57,8 @@ public class CustomTokenServices extends DefaultTokenServices {
tokenStore.removeAccessToken(existingAccessToken);
}
else {
// oidc每次授权都刷新id_token
existingAccessToken = refreshIdToken(existingAccessToken, authentication);
// Re-store the access token in case the authentication has changed
tokenStore.storeAccessToken(existingAccessToken, authentication);
return existingAccessToken;
@@ -89,6 +93,19 @@ public class CustomTokenServices extends DefaultTokenServices {
}
/**
* oidc每次授权都刷新id_token
* @param token 已存在的token
* @param authentication 认证信息
*/
private OAuth2AccessToken refreshIdToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
Set<String> responseTypes = authentication.getOAuth2Request().getResponseTypes();
if (accessTokenEnhancer != null && responseTypes.contains(SecurityConstants.ID_TOKEN)) {
return accessTokenEnhancer.enhance(token, authentication);
}
return token;
}
private OAuth2RefreshToken createRefreshToken(OAuth2Authentication authentication) {
if (!isSupportRefreshToken(authentication.getOAuth2Request())) {
return null;
@@ -100,6 +100,27 @@ public class OidcIdTokenBuilder {
return this.claim(SUB, subject);
}
/**
* 赋值用户姓名
*/
public OidcIdTokenBuilder name(String name) {
return this.claim(NAME, name);
}
/**
* 赋值用户登录名
*/
public OidcIdTokenBuilder loginName(String loginName) {
return this.claim(L_NAME, loginName);
}
/**
* 赋值用户头像
*/
public OidcIdTokenBuilder picture(String picture) {
return this.claim(PIC, picture);
}
/**
* Build the {@link OidcIdTokenBuilder}
*