MaxKeyConfig.java 8.7 KB
Newer Older
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
1
/*
M
220413  
MaxKey 已提交
2
 * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * 
 * 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.
 */
 

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

M
http  
MaxKey 已提交
20 21 22
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
23 24
import java.util.ArrayList;
import java.util.List;
M
http  
MaxKey 已提交
25
import java.util.stream.Collectors;
M
MaxKey 已提交
26

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
27
import org.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
M
v 3.3.0  
MaxKey 已提交
28
import org.maxkey.authn.realm.ldap.LdapAuthenticationRealmService;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
29 30
import org.maxkey.authn.support.kerberos.KerberosProxy;
import org.maxkey.authn.support.kerberos.RemoteKerberosService;
M
http  
MaxKey 已提交
31
import org.maxkey.configuration.EmailConfig;
M
v 3.3.0  
MaxKey 已提交
32
import org.maxkey.constants.ConstsPersistence;
M
MaxKey 已提交
33
import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
M
MaxKey 已提交
34
import org.maxkey.password.onetimepwd.MailOtpAuthnService;
35
import org.maxkey.password.onetimepwd.algorithm.OtpKeyUriFormat;
M
MaxKey 已提交
36 37 38
import org.maxkey.password.onetimepwd.impl.MailOtpAuthn;
import org.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
import org.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
39
import org.maxkey.persistence.redis.RedisConnectionFactory;
M
MaxKey 已提交
40 41 42
import org.maxkey.persistence.repository.LoginHistoryRepository;
import org.maxkey.persistence.repository.LoginRepository;
import org.maxkey.persistence.repository.PasswordPolicyValidator;
M
v 3.3.0  
MaxKey 已提交
43
import org.maxkey.persistence.service.LdapContextService;
M
MaxKey 已提交
44
import org.maxkey.persistence.service.UserInfoService;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
45 46
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
47
import org.springframework.beans.factory.InitializingBean;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
48
import org.springframework.beans.factory.annotation.Value;
M
MaxKey 已提交
49
import org.springframework.boot.autoconfigure.AutoConfiguration;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
50
import org.springframework.context.annotation.Bean;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
51
import org.springframework.context.annotation.ComponentScan;
M
http  
MaxKey 已提交
52 53
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
54
import org.springframework.jdbc.core.JdbcTemplate;
M
MaxKey 已提交
55
import org.springframework.security.crypto.password.PasswordEncoder;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
56

M
MaxKey 已提交
57
@AutoConfiguration
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
58
@ComponentScan(basePackages = {
M
MaxKey 已提交
59
		"org.maxkey.authn",
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
60
        "org.maxkey.configuration",
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
61 62 63 64 65
        "org.maxkey.domain",
        "org.maxkey.domain.apps",
        "org.maxkey.domain.userinfo",
        "org.maxkey.api.v1.contorller",
        "org.maxkey.web.endpoint",
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
66
        "org.maxkey.web.contorller",
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
67
        "org.maxkey.web.interceptor",
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
68 69 70 71 72 73
        //single sign on protocol
        "org.maxkey.authz.endpoint",
        "org.maxkey.authz.desktop.endpoint",
        "org.maxkey.authz.exapi.endpoint",
        "org.maxkey.authz.formbased.endpoint",
        "org.maxkey.authz.ltpa.endpoint",
74
        "org.maxkey.authz.token.endpoint"
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
75 76
})
public class MaxKeyConfig  implements InitializingBean {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
77
    private static final  Logger _logger = LoggerFactory.getLogger(MaxKeyConfig.class);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
78
    
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
79

M
MaxKey 已提交
80
    @Bean
81 82 83 84 85 86 87 88 89 90 91
    public OtpKeyUriFormat otpKeyUriFormat(
                @Value("${maxkey.otp.policy.type:totp}")
                String type,
                @Value("${maxkey.otp.policy.domain:MaxKey.top}")
                String domain,
                @Value("${maxkey.otp.policy.issuer:MaxKey}")
                String issuer,
                @Value("${maxkey.otp.policy.digits:6}")
                int digits,
                @Value("${maxkey.otp.policy.period:30}")
                int period) {
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
92
        
93 94 95
        OtpKeyUriFormat otpKeyUriFormat=new OtpKeyUriFormat(type,issuer,domain,digits,period);
        _logger.debug("OTP KeyUri Format " + otpKeyUriFormat);
        return otpKeyUriFormat;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
96
    }
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
97 98
    
    //可以在此实现其他的登陆认证方式,请实现AbstractAuthenticationRealm
M
MaxKey 已提交
99
    @Bean
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
100
    public JdbcAuthenticationRealm authenticationRealm(
M
MaxKey 已提交
101 102
    			PasswordEncoder passwordEncoder,
	    		PasswordPolicyValidator passwordPolicyValidator,
M
MaxKey 已提交
103 104
	    		LoginRepository loginService,
	    		LoginHistoryRepository loginHistoryService,
M
MaxKey 已提交
105
	    		UserInfoService userInfoService,
106
                JdbcTemplate jdbcTemplate,
M
MaxKey 已提交
107
                MailOtpAuthnService otpAuthnService,
M
v 3.3.0  
MaxKey 已提交
108 109
                LdapContextService ldapContextService) {
    	LdapAuthenticationRealmService ldapRealmService = new LdapAuthenticationRealmService(ldapContextService);
M
MaxKey 已提交
110 111 112 113 114
        JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm(
        		passwordEncoder,
        		passwordPolicyValidator,
        		loginService,
        		loginHistoryService,
M
MaxKey 已提交
115
        		userInfoService,
116
        		jdbcTemplate,
M
v 3.3.0  
MaxKey 已提交
117
        		ldapRealmService
M
MaxKey 已提交
118
        	);
M
MaxKey 已提交
119
        
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
120 121 122
        return authenticationRealm;
    }
    
M
MaxKey 已提交
123
	@Bean
124 125 126 127 128 129
    public TimeBasedOtpAuthn timeBasedOtpAuthn(
                @Value("${maxkey.otp.policy.digits:6}")
                int digits,
                @Value("${maxkey.otp.policy.period:30}")
                int period) {
	    TimeBasedOtpAuthn tfaOtpAuthn = new TimeBasedOtpAuthn(digits , period);
M
MaxKey 已提交
130
	    _logger.debug("TimeBasedOtpAuthn inited.");
M
Opt  
MaxKey 已提交
131
        return tfaOtpAuthn;
M
MaxKey 已提交
132 133
    }
    
M
MaxKey 已提交
134 135
    @Bean
    public AbstractOtpAuthn tfaOtpAuthn(
136 137 138 139 140 141 142 143
                @Value("${maxkey.login.mfa.type}")String mfaType,
                @Value("${maxkey.otp.policy.digits:6}")
                int digits,
                @Value("${maxkey.otp.policy.period:30}")
                int period,
                @Value("${maxkey.server.persistence}") int persistence,
                RedisConnectionFactory redisConnFactory) {    
        AbstractOtpAuthn tfaOtpAuthn  = new TimeBasedOtpAuthn(digits , period);
M
MaxKey 已提交
144 145
        _logger.debug("TimeBasedOtpAuthn inited.");

M
v 3.3.0  
MaxKey 已提交
146
        if (persistence == ConstsPersistence.REDIS) {
M
MaxKey 已提交
147
            RedisOtpTokenStore redisOptTokenStore = new RedisOtpTokenStore(redisConnFactory);
M
Opt  
MaxKey 已提交
148
            tfaOtpAuthn.setOptTokenStore(redisOptTokenStore);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
149 150
        }
        
M
Opt  
MaxKey 已提交
151 152
        tfaOtpAuthn.initPropertys();
        return tfaOtpAuthn;
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
153 154
    }
    
M
MaxKey 已提交
155
    @Bean
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
156
    public MailOtpAuthn mailOtpAuthn(
M
http  
MaxKey 已提交
157
            EmailConfig emailConfig,
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
158 159 160
            @Value("${spring.mail.properties.mailotp.message.subject}")
            String messageSubject,
            @Value("${spring.mail.properties.mailotp.message.template}")
M
http  
MaxKey 已提交
161 162 163 164 165
            String messageTemplate,
            @Value("${spring.mail.properties.mailotp.message.validity}")
            int messageValidity,
            @Value("${spring.mail.properties.mailotp.message.type}")
            String messageType
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
166
            ) {
M
http  
MaxKey 已提交
167 168 169 170 171 172 173 174 175 176 177
        if(messageType!= null && messageType.equalsIgnoreCase("html")) {
            Resource resource = new ClassPathResource("messages/email/forgotpassword.html");
            try {
                BufferedReader bufferedReader =new BufferedReader(new InputStreamReader(resource.getInputStream()));
                messageTemplate = bufferedReader.lines().collect(Collectors.joining("\n"));
                bufferedReader.close();
            } catch (IOException e) {
                 _logger.error("mailOtpAuthn IOException ",e);
            }
        }
        _logger.trace("messageTemplate \n" +messageTemplate);
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
178
        MailOtpAuthn mailOtpAuthn = new MailOtpAuthn();
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
179 180
        mailOtpAuthn.setSubject(messageSubject);
        mailOtpAuthn.setMessageTemplate(messageTemplate);
M
http  
MaxKey 已提交
181 182
        mailOtpAuthn.setEmailConfig(emailConfig);
        mailOtpAuthn.setInterval(messageValidity);
M
MaxKey 已提交
183
        _logger.debug("MailOtpAuthn inited.");
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
184 185 186
        return mailOtpAuthn;
    }
    
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
187
    
M
MaxKey 已提交
188
    @Bean
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
189
    public RemoteKerberosService kerberosService(
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
190
            @Value("${maxkey.login.kerberos.default.userdomain}")
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
191
            String userDomain,
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
192
            @Value("${maxkey.login.kerberos.default.fulluserdomain}")
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
193
            String fullUserDomain,
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
194
            @Value("${maxkey.login.kerberos.default.crypto}")
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
195
            String crypto,
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
196
            @Value("${maxkey.login.kerberos.default.redirecturi}")
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
            String redirectUri
            ) {
        RemoteKerberosService kerberosService = new RemoteKerberosService();
        KerberosProxy kerberosProxy = new KerberosProxy();
        
        kerberosProxy.setCrypto(crypto);
        kerberosProxy.setFullUserdomain(fullUserDomain);
        kerberosProxy.setUserdomain(userDomain);
        kerberosProxy.setRedirectUri(redirectUri);
        
        List<KerberosProxy> kerberosProxysList = new ArrayList<KerberosProxy>();
        kerberosProxysList.add(kerberosProxy);
        kerberosService.setKerberosProxys(kerberosProxysList);
        
        _logger.debug("RemoteKerberosService inited.");
        return kerberosService;
    }
    
215

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
216
    
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
217 218 219 220 221
    @Override
    public void afterPropertiesSet() throws Exception {
        
    }

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
222

MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
223
    
MaxKey单点登录官方's avatar
MaxKey单点登录官方 已提交
224
}