自定义AuthorizationCodeServices
package com.uwo.oss.security.oauth2.configuration;
import org.apache.log4j.Logger;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
import org.springframework.stereotype.Component;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* 主要是用来创建code与保存
* Created by yanhao on 2017/5/27.
*/
@Component
public class OssAusthorizationCodeServices implements AuthorizationCodeServices {
private static final Logger log = Logger.getLogger(OssAusthorizationCodeServices.class);
private final ConcurrentMap<String, OAuth2Authentication> codes = new ConcurrentHashMap<String, OAuth2Authentication>();
/**
* 获取code 与 销毁 等工作
* @param code
* @return
* @throws InvalidGrantException
*/
public OAuth2Authentication consumeAuthorizationCode(String code) throws InvalidGrantException {
log.warn("consume authorization code");
return codes.remove(code);
}
/**
* 创建code
* @param oAuth2Authentication
* @return
*/
public String createAuthorizationCode(OAuth2Authentication oAuth2Authentication) {
log.warn("create authorization code");
String code = UUID.randomUUID().toString().trim().replaceAll("-", "");
codes.put(code, oAuth2Authentication);
return code;
}
}
使用AuthorizationServerConfigurerAdapter配置
@Autowired
private OssUserDetailsService userDetailsService;
@Autowired
private OssAusthorizationCodeServices austhorizationCodeServices;
/**
* 用来定义授权与管理token
* @param endpoints
* @throws Exception
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
log.warn("configure AuthorizationServerEndpointsConfigurer");
endpoints
// 配置自定义code
.authorizationCodeServices(austhorizationCodeServices)
.tokenEnhancer(jwtTokenEnhancer())
.accessTokenConverter(jwtTokenEnhancer())
// 持久操作
.tokenStore(new RedisTokenStore(redisConnectionFactory))
.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService)
;
}
@Bean
protected JwtAccessTokenConverter jwtTokenEnhancer() {
OssJwtAccessTokenConverter converter = new OssJwtAccessTokenConverter();
converter.setSigningKey("uwo");
return converter;
}