Spring Boot 配置OAuth2 自定义code

自定义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;
    }

转载于:https://my.oschina.net/yan5845hao/blog/913454

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值