JwtAuthnAutoConfiguration.java 3.5 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
18 19 20 21 22
package org.maxkey.autoconfigure;

import com.nimbusds.jose.JOSEException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
23

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
import org.maxkey.authn.support.jwt.JwtLoginService;
import org.maxkey.crypto.jose.keystore.JWKSetKeyStore;
import org.maxkey.crypto.jwt.signer.service.impl.DefaultJwtSigningAndValidationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;


@Configuration
public class JwtAuthnAutoConfiguration implements InitializingBean {
    private static final  Logger _logger = LoggerFactory.getLogger(JwtAuthnAutoConfiguration.class);

    /**
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
41
     * jwt Login JwkSetKeyStore.
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
42 43
     * @return
     */
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
44 45
    @Bean(name = "jwtLoginJwkSetKeyStore")
    public JWKSetKeyStore jwtLoginJwkSetKeyStore() {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
46
        JWKSetKeyStore jwkSetKeyStore = new JWKSetKeyStore();
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
47
        ClassPathResource classPathResource = new ClassPathResource("/config/loginjwkkeystore.jwks");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
48
        jwkSetKeyStore.setLocation(classPathResource);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
49
        _logger.debug("JWT Login JwkSet KeyStore init.");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
50 51 52 53
        return jwkSetKeyStore;
    }
    
    /**
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
54
     * jwt Login ValidationService.
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
55 56 57 58 59
     * @return
     * @throws JOSEException
     * @throws InvalidKeySpecException 
     * @throws NoSuchAlgorithmException 
     */
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
60 61 62
    @Bean(name = "jwtLoginValidationService")
    public DefaultJwtSigningAndValidationService jwtLoginValidationService(
            JWKSetKeyStore jwtLoginJwkSetKeyStore) 
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
63 64
                    throws NoSuchAlgorithmException, InvalidKeySpecException, JOSEException {
        DefaultJwtSigningAndValidationService jwtSignerValidationService = 
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
65
                new DefaultJwtSigningAndValidationService(jwtLoginJwkSetKeyStore);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
66 67
        jwtSignerValidationService.setDefaultSignerKeyId("maxkey_rsa");
        jwtSignerValidationService.setDefaultSigningAlgorithmName("RS256");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
68
        _logger.debug("JWT Login Signing and Validation init.");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
69 70
        return jwtSignerValidationService;
    }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
71

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
72
    /**
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
73
     * Jwt LoginService.
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
74 75 76 77
     * @return
     */
    @Bean(name = "jwtLoginService")
    public JwtLoginService jwtLoginService(
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
78 79 80
            @Value("${maxkey.login.jwt.issuer}")
            String issuer,
            DefaultJwtSigningAndValidationService jwtLoginValidationService) {
81
        JwtLoginService jwtLoginService = new JwtLoginService(
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
82 83
                    jwtLoginValidationService,
                    issuer
84
                );
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
85
        _logger.debug("JWT Login Service init.");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
86
        return jwtLoginService;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
87 88 89 90 91 92 93 94 95
    }
    
 
    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        
    }
}