SslConfig

This commit is contained in:
shimingxy 2020-06-28 10:39:45 +08:00
parent 0ce43cc06d
commit b086675e2b
9 changed files with 78 additions and 36 deletions

View File

@ -60,6 +60,7 @@ public class BasicEntryPoint extends HandlerInterceptorAdapter {
// session not existssession timeoutrecreate new session // session not existssession timeoutrecreate new session
if(request.getSession(false) == null) { if(request.getSession(false) == null) {
_logger.info("recreate new session .");
request.getSession(true); request.getSession(true);
} }
String basicCredential =request.getHeader(AuthorizationHeaderUtils.AUTHORIZATION_HEADERNAME); String basicCredential =request.getHeader(AuthorizationHeaderUtils.AUTHORIZATION_HEADERNAME);

View File

@ -52,6 +52,7 @@ public class HttpHeaderEntryPoint extends HandlerInterceptorAdapter {
// session not existssession timeoutrecreate new session // session not existssession timeoutrecreate new session
if(request.getSession(false) == null) { if(request.getSession(false) == null) {
_logger.info("recreate new session .");
request.getSession(true); request.getSession(true);
} }

View File

@ -123,8 +123,11 @@ public abstract class AbstractAuthenticationProvider {
*/ */
protected void sessionValid(String sessionId) { protected void sessionValid(String sessionId) {
if (sessionId == null || !sessionId.equals(WebContext.getSession().getId())) { if (sessionId == null || !sessionId.equals(WebContext.getSession().getId())) {
String message = WebContext.getI18nValue("login.error.session");
_logger.debug("login session valid error."); _logger.debug("login session valid error.");
_logger.debug("login session sessionId " + sessionId);
_logger.debug("login getSession sessionId " + WebContext.getSession().getId());
String message = WebContext.getI18nValue("login.error.session");
throw new BadCredentialsException(message); throw new BadCredentialsException(message);
} }
} }

View File

@ -149,7 +149,7 @@ public class MvcAutoConfiguration implements InitializingBean {
new MappingJackson2HttpMessageConverter(); new MappingJackson2HttpMessageConverter();
ArrayList<MediaType> mediaTypesList = new ArrayList<MediaType>(); ArrayList<MediaType> mediaTypesList = new ArrayList<MediaType>();
mediaTypesList.add(MediaType.APPLICATION_JSON); mediaTypesList.add(MediaType.APPLICATION_JSON);
mediaTypesList.add(MediaType.TEXT_PLAIN); //mediaTypesList.add(MediaType.TEXT_PLAIN);
mappingJacksonHttpMessageConverter.setSupportedMediaTypes(mediaTypesList); mappingJacksonHttpMessageConverter.setSupportedMediaTypes(mediaTypesList);
return mappingJacksonHttpMessageConverter; return mappingJacksonHttpMessageConverter;
} }

View File

@ -220,6 +220,7 @@ public final class WebContext {
* @return HttpSession * @return HttpSession
*/ */
public static HttpSession getSession(boolean create) { public static HttpSession getSession(boolean create) {
System.out.println("new Session created");
return getRequest().getSession(create); return getRequest().getSession(create);
} }

View File

@ -1,6 +1,7 @@
package org.maxkey; package org.maxkey;
import java.util.Date; import java.util.Date;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import org.apache.ibatis.io.VFS; import org.apache.ibatis.io.VFS;
import org.apache.mybatis.jpa.SpringBootVFS; import org.apache.mybatis.jpa.SpringBootVFS;
@ -43,5 +44,13 @@ public class MaxKeyApplication extends SpringBootServletInitializer {
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MaxKeyApplication.class); return application.sources(MaxKeyApplication.class);
} }
/*@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
SessionCookieConfig sessionCookieConfig = servletContext.getSessionCookieConfig();
sessionCookieConfig.setHttpOnly(true);
}*/
} }

View File

@ -2,10 +2,6 @@ package org.maxkey;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm; import org.maxkey.authn.realm.jdbc.JdbcAuthenticationRealm;
import org.maxkey.authn.realm.ldap.LdapAuthenticationRealm; import org.maxkey.authn.realm.ldap.LdapAuthenticationRealm;
import org.maxkey.authn.realm.ldap.LdapServer; import org.maxkey.authn.realm.ldap.LdapServer;
@ -31,7 +27,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
@ -77,35 +72,7 @@ public class MaxKeyConfig implements InitializingBean {
registration.setOrder(1); registration.setOrder(1);
return registration; return registration;
} }
@Bean
public Connector connector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(80);
connector.setSecure(false);
connector.setRedirectPort(443);
return connector;
}
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
@Bean(name = "keyUriFormat") @Bean(name = "keyUriFormat")
public KeyUriFormat keyUriFormat( public KeyUriFormat keyUriFormat(
@Value("${config.otp.keyuri.format.type:totp}") @Value("${config.otp.keyuri.format.type:totp}")

View File

@ -0,0 +1,60 @@
package org.maxkey;
import org.apache.catalina.Context;
import org.apache.catalina.connector.Connector;
import org.apache.tomcat.util.descriptor.web.SecurityCollection;
import org.apache.tomcat.util.descriptor.web.SecurityConstraint;
import org.maxkey.constants.ConstantsProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
//@ImportResource(locations = { "classpath:spring/maxkey.xml" })
@PropertySource(ConstantsProperties.applicationPropertySource)
@PropertySource(ConstantsProperties.maxKeyPropertySource)
public class MaxKeySslConfig implements InitializingBean {
private static final Logger _logger = LoggerFactory.getLogger(MaxKeySslConfig.class);
@Bean
public Connector connector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(80);
connector.setSecure(true);
//connector.setRedirectPort(443);
_logger.debug("Ssl Support .");
return connector;
}
@Bean
public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector) {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(connector);
return tomcat;
}
@Override
public void afterPropertiesSet() throws Exception {
}
}

View File

@ -5,7 +5,7 @@
config.server.domain=maxkey.top config.server.domain=maxkey.top
config.server.domain.sub=sso.${config.server.domain} config.server.domain.sub=sso.${config.server.domain}
config.server.name=http://${config.server.domain.sub} config.server.name=http://${config.server.domain.sub}
config.server.prefix.uri=${config.server.name}/maxkey config.server.prefix.uri=${config.server.name}:80/maxkey
#default.uri #default.uri
config.server.default.uri=${config.server.prefix.uri}/maxkey/appList config.server.default.uri=${config.server.prefix.uri}/maxkey/appList
config.server.management.uri=${config.server.name}:9521/maxkey-mgt/login config.server.management.uri=${config.server.name}:9521/maxkey-mgt/login