AuthenticationAutoConfiguration.java 8.3 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 20 21
 * 
 * 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;

import org.maxkey.authn.AbstractAuthenticationProvider;
import org.maxkey.authn.SavedRequestAwareAuthenticationSuccessHandler;
M
MaxKey 已提交
22
import org.maxkey.authn.jwt.AuthJwtService;
M
MaxKey 已提交
23 24 25
import org.maxkey.authn.jwt.CongressService;
import org.maxkey.authn.jwt.InMemoryCongressService;
import org.maxkey.authn.jwt.RedisCongressService;
M
MaxKey 已提交
26 27
import org.maxkey.authn.online.OnlineTicketService;
import org.maxkey.authn.online.OnlineTicketServiceFactory;
M
MaxKey 已提交
28
import org.maxkey.authn.provider.AuthenticationProviderFactory;
M
MaxKey 已提交
29 30 31
import org.maxkey.authn.provider.MobileAuthenticationProvider;
import org.maxkey.authn.provider.NormalAuthenticationProvider;
import org.maxkey.authn.provider.TrustedAuthenticationProvider;
M
MaxKey 已提交
32
import org.maxkey.authn.realm.AbstractAuthenticationRealm;
M
authn  
MaxKey 已提交
33
import org.maxkey.authn.web.SessionListenerAdapter;
M
MaxKey 已提交
34
import org.maxkey.configuration.ApplicationConfig;
M
MaxKey 已提交
35
import org.maxkey.configuration.AuthJwkConfig;
M
v 3.3.0  
MaxKey 已提交
36
import org.maxkey.constants.ConstsPersistence;
M
MaxKey 已提交
37
import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
M
v 3.3.0  
MaxKey 已提交
38 39
import org.maxkey.password.onetimepwd.OtpAuthnService;
import org.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
M
MaxKey 已提交
40
import org.maxkey.persistence.MomentaryService;
M
MaxKey 已提交
41
import org.maxkey.persistence.redis.RedisConnectionFactory;
M
MaxKey 已提交
42 43 44
import org.maxkey.persistence.repository.LoginHistoryRepository;
import org.maxkey.persistence.repository.LoginRepository;
import org.maxkey.persistence.repository.PasswordPolicyValidator;
M
v 3.3.0  
MaxKey 已提交
45 46
import org.maxkey.persistence.service.EmailSendersService;
import org.maxkey.persistence.service.SmsProviderService;
M
MaxKey 已提交
47 48 49 50 51 52 53 54 55
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.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

M
MaxKey 已提交
56 57
import com.nimbusds.jose.JOSEException;

M
MaxKey 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71

@Configuration
public class AuthenticationAutoConfiguration  implements InitializingBean {
    private static final  Logger _logger = 
            LoggerFactory.getLogger(AuthenticationAutoConfiguration.class);
    
    
    @Bean(name = "savedRequestSuccessHandler")
    public SavedRequestAwareAuthenticationSuccessHandler 
            savedRequestAwareAuthenticationSuccessHandler() {
        return new SavedRequestAwareAuthenticationSuccessHandler();
    }
    
    @Bean(name = "authenticationProvider")
M
MaxKey 已提交
72
    public AbstractAuthenticationProvider authenticationProvider(
M
MaxKey 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86
    		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 已提交
87 88
    		AbstractAuthenticationRealm authenticationRealm,
    		ApplicationConfig applicationConfig,
M
MaxKey 已提交
89 90 91
    	    OnlineTicketService onlineTicketServices,
    	    AuthJwtService authJwtService,
    	    MomentaryService momentaryService
M
MaxKey 已提交
92
    		) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
93
    	_logger.debug("init authentication Provider .");
M
MaxKey 已提交
94
    	return new NormalAuthenticationProvider(
M
MaxKey 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107
        		authenticationRealm,
        		applicationConfig,
        		onlineTicketServices,
        		authJwtService,
        		momentaryService
        	);
    }
    
    @Bean(name = "mobileAuthenticationProvider")
    public AbstractAuthenticationProvider mobileAuthenticationProvider(
    		AbstractAuthenticationRealm authenticationRealm,
    		ApplicationConfig applicationConfig,
    	    OtpAuthnService otpAuthnService,
M
MaxKey 已提交
108
    	    OnlineTicketService onlineTicketServices
M
MaxKey 已提交
109
    		) {
M
MaxKey 已提交
110 111
    	_logger.debug("init Mobile authentication Provider .");
    	return new MobileAuthenticationProvider(
M
MaxKey 已提交
112 113
        		authenticationRealm,
        		applicationConfig,
M
v 3.3.0  
MaxKey 已提交
114
        		otpAuthnService,
M
MaxKey 已提交
115
        		onlineTicketServices
M
MaxKey 已提交
116 117
        	);
    }
M
MaxKey 已提交
118

M
MaxKey 已提交
119 120 121 122
    @Bean(name = "trustedAuthenticationProvider")
    public AbstractAuthenticationProvider trustedAuthenticationProvider(
    		AbstractAuthenticationRealm authenticationRealm,
    		ApplicationConfig applicationConfig,
M
MaxKey 已提交
123
    	    OnlineTicketService onlineTicketServices
M
MaxKey 已提交
124
    		) {
M
MaxKey 已提交
125 126
    	_logger.debug("init Mobile authentication Provider .");
    	return new TrustedAuthenticationProvider(
M
MaxKey 已提交
127 128 129 130
        		authenticationRealm,
        		applicationConfig,
        		onlineTicketServices
        	);
M
MaxKey 已提交
131 132
    }
    
M
MaxKey 已提交
133
    @Bean(name = "authJwtService")
M
MaxKey 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146
    public AuthJwtService authJwtService(
    		AuthJwkConfig authJwkConfig,
    		RedisConnectionFactory redisConnFactory,
    		@Value("${maxkey.server.persistence}") int persistence) throws JOSEException {
    	CongressService congressService;
    	if (persistence == ConstsPersistence.REDIS) {
    		congressService = new RedisCongressService(redisConnFactory);
    	}else {
    		congressService = new InMemoryCongressService();
    	}
    	
    	AuthJwtService authJwtService = new AuthJwtService(authJwkConfig,congressService);
    	
M
MaxKey 已提交
147 148 149
    	return authJwtService;
    }
    
M
v 3.3.0  
MaxKey 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    @Bean(name = "otpAuthnService")
    public OtpAuthnService otpAuthnService(
            @Value("${maxkey.server.persistence}") int persistence,
            SmsProviderService smsProviderService,
            EmailSendersService emailSendersService,
            RedisConnectionFactory redisConnFactory) {
        OtpAuthnService otpAuthnService = 
        							new OtpAuthnService(smsProviderService,emailSendersService);
        
        if (persistence == ConstsPersistence.REDIS) {
            RedisOtpTokenStore redisOptTokenStore = new RedisOtpTokenStore(redisConnFactory);
            otpAuthnService.setRedisOptTokenStore(redisOptTokenStore);
        }
        
        
        _logger.debug("OneTimePasswordService {} inited." , 
        				persistence == ConstsPersistence.REDIS ? "Redis" : "InMemory");
        return otpAuthnService;
    }
    
M
MaxKey 已提交
170 171 172 173 174
    @Bean(name = "passwordPolicyValidator")
    public PasswordPolicyValidator passwordPolicyValidator(JdbcTemplate jdbcTemplate,MessageSource messageSource) {
        return new PasswordPolicyValidator(jdbcTemplate,messageSource);
    }
    
M
MaxKey 已提交
175 176 177
    @Bean(name = "loginRepository")
    public LoginRepository loginRepository(JdbcTemplate jdbcTemplate) {
        return new LoginRepository(jdbcTemplate);
M
MaxKey 已提交
178
    }
M
MaxKey 已提交
179 180 181
    @Bean(name = "loginHistoryRepository")
    public LoginHistoryRepository LoginHistoryRepository(JdbcTemplate jdbcTemplate) {
        return new LoginHistoryRepository(jdbcTemplate);
M
MaxKey 已提交
182 183 184
    }
    
    
M
MaxKey 已提交
185 186
    @Bean(name = "onlineTicketService")
    public OnlineTicketService onlineTicketService(
M
MaxKey 已提交
187
            @Value("${maxkey.server.persistence}") int persistence,
M
MaxKey 已提交
188
            JdbcTemplate jdbcTemplate,
M
MaxKey 已提交
189 190 191
            RedisConnectionFactory redisConnFactory,
            @Value("${server.servlet.session.timeout:1800}") int timeout
            ) {
M
MaxKey 已提交
192 193 194
        OnlineTicketService  onlineTicketService  = 
                new OnlineTicketServiceFactory().getService(persistence, jdbcTemplate, redisConnFactory);
        onlineTicketService.setValiditySeconds(timeout);
M
MaxKey 已提交
195
        _logger.trace("onlineTicket timeout " + timeout);
M
MaxKey 已提交
196
        return onlineTicketService;
M
MaxKey 已提交
197 198
    }
    
M
authn  
MaxKey 已提交
199
    @Bean(name = "sessionListenerAdapter")
M
220413  
MaxKey 已提交
200 201
    public SessionListenerAdapter sessionListenerAdapter() {
        return new SessionListenerAdapter();
M
authn  
MaxKey 已提交
202 203
    }
    
M
MaxKey 已提交
204 205 206 207 208
    @Override
    public void afterPropertiesSet() throws Exception {
        
    }
}