kaptcha

上级 b099b40c
package org.maxkey;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.servlet.ServletException;
import org.maxkey.authn.SavedRequestAwareAuthenticationSuccessHandler;
import org.maxkey.crypto.password.PasswordReciprocal;
import org.maxkey.web.InitializeContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
@SpringBootApplication
@ImportResource(locations={"classpath:spring/maxkey-mgt.xml"})
......@@ -25,11 +39,6 @@ import org.springframework.context.annotation.ImportResource;
public class MaxKeyMgtApplication extends SpringBootServletInitializer {
private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMgtApplication.class);
@Bean
MaxKeyMgtConfig MaxKeyMgtConfig() {
return new MaxKeyMgtConfig();
}
public static void main(String[] args) {
System.out.println("MaxKeyMgtApplication");
......@@ -54,5 +63,57 @@ public class MaxKeyMgtApplication extends SpringBootServletInitializer {
return application.sources(MaxKeyMgtApplication.class);
}
@Bean
MaxKeyMgtConfig MaxKeyMgtConfig() {
return new MaxKeyMgtConfig();
}
/**
* 配置默认错误页面(仅用于内嵌tomcat启动时)
* 使用这种方式,在打包为war后不起作用
*
* @return
*/
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
@Override
public void customize(ConfigurableWebServerFactory factory) {
ErrorPage errorPage400 = new ErrorPage(HttpStatus.BAD_REQUEST,"/exception/error/400");
ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND,"/exception/error/404");
ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/exception/error/500");
factory.addErrorPages(errorPage400, errorPage404,errorPage500);
}
};
}
@Bean(name = "passwordReciprocal")
public PasswordReciprocal passwordReciprocal() {
return new PasswordReciprocal();
}
@Bean(name = "savedRequestSuccessHandler")
public SavedRequestAwareAuthenticationSuccessHandler SavedRequestAwareAuthenticationSuccessHandler() {
return new SavedRequestAwareAuthenticationSuccessHandler();
}
/**
* Captcha Producer Config .
* @return Producer
* @throws IOException
*/
@Bean(name = "captchaProducer")
public Producer captchaProducer() throws IOException{
Resource resource = new ClassPathResource("config/kaptcha.properties");
_logger.debug("Kaptcha config file " + resource.getURL());
DefaultKaptcha kaptcha=new DefaultKaptcha();
Properties properties = new Properties();
properties.load(resource.getInputStream());
Config config = new Config(properties);
kaptcha.setConfig(config);
return kaptcha;
}
}
package org.maxkey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
@Configuration
@Component
@PropertySource("classpath:/application.properties")
public class MaxKeyMgtConfig {
private static final Logger _logger = LoggerFactory.getLogger(MaxKeyMgtConfig.class);
@Value("${server.port:8080}")
private int port;
......@@ -25,23 +21,5 @@ public class MaxKeyMgtConfig {
this.port = port;
}
/**
* 配置默认错误页面(仅用于内嵌tomcat启动时)
* 使用这种方式,在打包为war后不起作用
*
* @return
*/
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
@Override
public void customize(ConfigurableWebServerFactory factory) {
ErrorPage errorPage400 = new ErrorPage(HttpStatus.BAD_REQUEST,"/exception/error/400");
ErrorPage errorPage404 = new ErrorPage(HttpStatus.NOT_FOUND,"/exception/error/404");
ErrorPage errorPage500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/exception/error/500");
factory.addErrorPages(errorPage400, errorPage404,errorPage500);
}
};
}
}
kaptcha.image.width=80
kaptcha.image.height=25
kaptcha.border=no
kaptcha.obscurificator.impl=com.google.code.kaptcha.impl.ShadowGimpy
kaptcha.textproducer.font.size=23
kaptcha.textproducer.char.string=0123456789
kaptcha.textproducer.char.length=4
kaptcha.noise.impl=com.google.code.kaptcha.impl.NoNoise
#kaptcha.noise.color=white
\ No newline at end of file
......@@ -113,31 +113,6 @@
<!-- Authentication Password Encoder Config -->
<bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"></bean>
<bean id="passwordReciprocal" class="org.maxkey.crypto.password.PasswordReciprocal"></bean>
<!-- Captcha Producer Config -->
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
<property name="config" >
<bean id="kaptchaConfig" class="com.google.code.kaptcha.util.Config">
<constructor-arg type="java.util.Properties">
<props>
<prop key="kaptcha.image.width">80</prop>
<prop key="kaptcha.image.height">25</prop>
<prop key="kaptcha.border">no</prop>
<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.ShadowGimpy</prop>
<prop key="kaptcha.textproducer.font.size">23</prop>
<prop key="kaptcha.textproducer.char.string">0123456789</prop>
<prop key="kaptcha.textproducer.char.length">4</prop>
<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop>
<!-- <prop key="kaptcha.noise.color">white</prop> -->
</props>
</constructor-arg>
</bean>
</property>
</bean>
<bean id="savedRequestSuccessHandler" class="org.maxkey.authn.SavedRequestAwareAuthenticationSuccessHandler"> </bean>
<!-- LDAP Realm
<bean id="authenticationRealm" class="org.maxkey.web.authentication.realm.ldap.LdapAuthenticationRealm">
<constructor-arg ref="jdbcTemplate"/>
......
......@@ -56,7 +56,7 @@
<constructor-arg value="${config.maxkey.uri}"/>
</bean>
<bean id="oauth20JdbcClientDetailsService" class="org.maxkey.authz.oauth2.provider.client.JdbcClientDetailsService">
<bean id="oauth20JdbcClientDetailsService" class="org.maxkey.authz.oauth2.provider.client.JdbcClientDetailsService" >
<constructor-arg ref="dataSource" />
<property name="passwordEncoder" ref="passwordReciprocal"></property>
</bean>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册