AuthnProviderAutoConfiguration.java 5.4 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
MaxKey 已提交
31
import org.maxkey.password.sms.SmsOtpAuthnService;
M
MaxKey 已提交
32 33 34
import org.maxkey.persistence.repository.LoginHistoryRepository;
import org.maxkey.persistence.repository.LoginRepository;
import org.maxkey.persistence.repository.PasswordPolicyValidator;
M
MaxKey 已提交
35 36 37 38
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
M
MaxKey 已提交
39
import org.springframework.boot.autoconfigure.AutoConfiguration;
M
MaxKey 已提交
40 41 42 43 44
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;


M
MaxKey 已提交
45
@AutoConfiguration
M
MaxKey 已提交
46
public class AuthnProviderAutoConfiguration  implements InitializingBean {
M
MaxKey 已提交
47
    private static final  Logger _logger = 
M
MaxKey 已提交
48
            LoggerFactory.getLogger(AuthnProviderAutoConfiguration.class);
M
MaxKey 已提交
49
    
M
MaxKey 已提交
50
    @Bean
M
MaxKey 已提交
51
    public AbstractAuthenticationProvider authenticationProvider(
M
MaxKey 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65
    		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 已提交
66 67
    		AbstractAuthenticationRealm authenticationRealm,
    		ApplicationConfig applicationConfig,
M
MaxKey 已提交
68
    	    SessionManager sessionManager,
M
MaxKey 已提交
69
    	    AuthTokenService authTokenService
M
MaxKey 已提交
70
    		) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
71
    	_logger.debug("init authentication Provider .");
M
MaxKey 已提交
72
    	return new NormalAuthenticationProvider(
M
MaxKey 已提交
73 74
        		authenticationRealm,
        		applicationConfig,
M
MaxKey 已提交
75
        		sessionManager,
M
MaxKey 已提交
76
        		authTokenService
M
MaxKey 已提交
77 78 79
        	);
    }
    
M
MaxKey 已提交
80
    @Bean
M
MaxKey 已提交
81 82 83
    public AbstractAuthenticationProvider mobileAuthenticationProvider(
    		AbstractAuthenticationRealm authenticationRealm,
    		ApplicationConfig applicationConfig,
M
MaxKey 已提交
84
    	    SmsOtpAuthnService smsAuthnService,
M
MaxKey 已提交
85
    	    SessionManager sessionManager
M
MaxKey 已提交
86
    		) {
M
MaxKey 已提交
87 88
    	_logger.debug("init Mobile authentication Provider .");
    	return new MobileAuthenticationProvider(
M
MaxKey 已提交
89 90
        		authenticationRealm,
        		applicationConfig,
M
MaxKey 已提交
91
        		smsAuthnService,
M
MaxKey 已提交
92
        		sessionManager
M
MaxKey 已提交
93 94
        	);
    }
M
MaxKey 已提交
95

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