AuthnProviderAutoConfiguration.java 5.7 KB
Newer Older
M
MaxKey 已提交
1
/*
M
MaxKey 已提交
2
 * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
M
MaxKey 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 
 * 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.
 */
 

package org.maxkey.autoconfigure;

M
MaxKey 已提交
20
import org.maxkey.authn.jwt.AuthTokenService;
M
MaxKey 已提交
21
import org.maxkey.authn.provider.AbstractAuthenticationProvider;
M
MaxKey 已提交
22
import org.maxkey.authn.provider.AuthenticationProviderFactory;
M
MaxKey 已提交
23 24 25
import org.maxkey.authn.provider.impl.MobileAuthenticationProvider;
import org.maxkey.authn.provider.impl.NormalAuthenticationProvider;
import org.maxkey.authn.provider.impl.TrustedAuthenticationProvider;
M
MaxKey 已提交
26
import org.maxkey.authn.realm.AbstractAuthenticationRealm;
M
MaxKey 已提交
27
import org.maxkey.authn.session.SessionManager;
M
MaxKey 已提交
28 29
import org.maxkey.authn.support.rememberme.AbstractRemeberMeManager;
import org.maxkey.authn.support.rememberme.JdbcRemeberMeManager;
M
MaxKey 已提交
30
import org.maxkey.configuration.ApplicationConfig;
M
v 3.3.0  
MaxKey 已提交
31 32 33
import org.maxkey.constants.ConstsPersistence;
import org.maxkey.password.onetimepwd.OtpAuthnService;
import org.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
M
MaxKey 已提交
34
import org.maxkey.persistence.redis.RedisConnectionFactory;
M
MaxKey 已提交
35 36 37
import org.maxkey.persistence.repository.LoginHistoryRepository;
import org.maxkey.persistence.repository.LoginRepository;
import org.maxkey.persistence.repository.PasswordPolicyValidator;
M
v 3.3.0  
MaxKey 已提交
38 39
import org.maxkey.persistence.service.EmailSendersService;
import org.maxkey.persistence.service.SmsProviderService;
M
MaxKey 已提交
40 41 42 43
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
M
MaxKey 已提交
44
import org.springframework.boot.autoconfigure.AutoConfiguration;
M
MaxKey 已提交
45 46 47 48 49
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;


M
MaxKey 已提交
50
@AutoConfiguration
M
MaxKey 已提交
51
public class AuthnProviderAutoConfiguration  implements InitializingBean {
M
MaxKey 已提交
52
    private static final  Logger _logger = 
M
MaxKey 已提交
53
            LoggerFactory.getLogger(AuthnProviderAutoConfiguration.class);
M
MaxKey 已提交
54
    
M
MaxKey 已提交
55
    @Bean
M
MaxKey 已提交
56
    public AbstractAuthenticationProvider authenticationProvider(
M
MaxKey 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70
    		AbstractAuthenticationProvider normalAuthenticationProvider,
    		AbstractAuthenticationProvider mobileAuthenticationProvider,
    		AbstractAuthenticationProvider trustedAuthenticationProvider
    		) {
    	AuthenticationProviderFactory authenticationProvider = new AuthenticationProviderFactory();
    	authenticationProvider.addAuthenticationProvider(normalAuthenticationProvider);
    	authenticationProvider.addAuthenticationProvider(mobileAuthenticationProvider);
    	authenticationProvider.addAuthenticationProvider(trustedAuthenticationProvider);
    	
    	return authenticationProvider;
    }
    		
    @Bean
    public AbstractAuthenticationProvider normalAuthenticationProvider(
M
MaxKey 已提交
71 72
    		AbstractAuthenticationRealm authenticationRealm,
    		ApplicationConfig applicationConfig,
M
MaxKey 已提交
73
    	    SessionManager sessionManager,
M
MaxKey 已提交
74
    	    AuthTokenService authTokenService
M
MaxKey 已提交
75
    		) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
76
    	_logger.debug("init authentication Provider .");
M
MaxKey 已提交
77
    	return new NormalAuthenticationProvider(
M
MaxKey 已提交
78 79
        		authenticationRealm,
        		applicationConfig,
M
MaxKey 已提交
80
        		sessionManager,
M
MaxKey 已提交
81
        		authTokenService
M
MaxKey 已提交
82 83 84
        	);
    }
    
M
MaxKey 已提交
85
    @Bean
M
MaxKey 已提交
86 87 88 89
    public AbstractAuthenticationProvider mobileAuthenticationProvider(
    		AbstractAuthenticationRealm authenticationRealm,
    		ApplicationConfig applicationConfig,
    	    OtpAuthnService otpAuthnService,
M
MaxKey 已提交
90
    	    SessionManager sessionManager
M
MaxKey 已提交
91
    		) {
M
MaxKey 已提交
92 93
    	_logger.debug("init Mobile authentication Provider .");
    	return new MobileAuthenticationProvider(
M
MaxKey 已提交
94 95
        		authenticationRealm,
        		applicationConfig,
M
v 3.3.0  
MaxKey 已提交
96
        		otpAuthnService,
M
MaxKey 已提交
97
        		sessionManager
M
MaxKey 已提交
98 99
        	);
    }
M
MaxKey 已提交
100

M
MaxKey 已提交
101
    @Bean
M
MaxKey 已提交
102 103 104
    public AbstractAuthenticationProvider trustedAuthenticationProvider(
    		AbstractAuthenticationRealm authenticationRealm,
    		ApplicationConfig applicationConfig,
M
MaxKey 已提交
105
    	    SessionManager sessionManager
M
MaxKey 已提交
106
    		) {
M
MaxKey 已提交
107 108
    	_logger.debug("init Mobile authentication Provider .");
    	return new TrustedAuthenticationProvider(
M
MaxKey 已提交
109 110
        		authenticationRealm,
        		applicationConfig,
M
MaxKey 已提交
111
        		sessionManager
M
MaxKey 已提交
112
        	);
M
MaxKey 已提交
113 114
    }
    
M
MaxKey 已提交
115
    @Bean
M
MaxKey 已提交
116 117 118 119
    public PasswordPolicyValidator passwordPolicyValidator(JdbcTemplate jdbcTemplate,MessageSource messageSource) {
        return new PasswordPolicyValidator(jdbcTemplate,messageSource);
    }
    
M
MaxKey 已提交
120
    @Bean
M
MaxKey 已提交
121 122
    public LoginRepository loginRepository(JdbcTemplate jdbcTemplate) {
        return new LoginRepository(jdbcTemplate);
M
MaxKey 已提交
123
    }
M
MaxKey 已提交
124
    
M
MaxKey 已提交
125 126
    @Bean
    public LoginHistoryRepository loginHistoryRepository(JdbcTemplate jdbcTemplate) {
M
MaxKey 已提交
127
        return new LoginHistoryRepository(jdbcTemplate);
M
MaxKey 已提交
128 129
    }
    
M
MaxKey 已提交
130 131 132 133 134
    /**
     * remeberMeService .
     * @return
     */
    @Bean
M
MaxKey 已提交
135
    public AbstractRemeberMeManager remeberMeManager(
M
MaxKey 已提交
136 137 138
            @Value("${maxkey.server.persistence}") int persistence,
            @Value("${maxkey.login.remeberme.validity}") int validity,
            ApplicationConfig applicationConfig,
M
MaxKey 已提交
139
            AuthTokenService authTokenService,
M
MaxKey 已提交
140
            JdbcTemplate jdbcTemplate) {
M
MaxKey 已提交
141 142 143
    	_logger.trace("init RemeberMeManager , validity {}." , validity);
        return new  JdbcRemeberMeManager(
        		jdbcTemplate,applicationConfig,authTokenService,validity);
M
MaxKey 已提交
144 145
    }
    
M
MaxKey 已提交
146 147 148 149 150
    @Override
    public void afterPropertiesSet() throws Exception {
        
    }
}