com.centit.framework.config.SpringSecurityBaseConfig Maven / Gradle / Ivy
package com.centit.framework.config;
import com.centit.framework.security.*;
import com.centit.framework.security.model.CentitSecurityMetadata;
import com.centit.framework.security.model.CentitSessionRegistry;
import com.centit.framework.security.model.CentitUserDetailsService;
import com.centit.support.algorithm.BooleanBaseOpt;
import com.centit.support.algorithm.StringBaseOpt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CsrfTokenRepository;
import org.springframework.security.web.firewall.StrictHttpFirewall;
/**
* Created by zou_wy on 2017/3/29.
*/
public abstract class SpringSecurityBaseConfig extends WebSecurityConfigurerAdapter {
@Autowired
protected Environment env;
@Autowired
protected CsrfTokenRepository csrfTokenRepository;
@Autowired
protected CentitSessionRegistry centitSessionRegistry;
@Autowired
protected CentitUserDetailsService centitUserDetailsService;
@Override
public void configure(WebSecurity web) throws Exception {
String ignoreUrl = StringUtils.deleteWhitespace(env.getProperty("security.ignore.url"));
if(StringUtils.isNotBlank(ignoreUrl)){
String[] ignoreUrls = ignoreUrl.split(",");
for(int i = 0; i < ignoreUrls.length; i++){
web.ignoring().antMatchers(ignoreUrls[i]);
}
}
web.httpFirewall(httpFirewall());
}
protected DaoFilterSecurityInterceptor createCentitPowerFilter(AuthenticationManager authenticationManager,
DaoAccessDecisionManager centitAccessDecisionManagerBean,
DaoInvocationSecurityMetadataSource centitSecurityMetadataSource) {
DaoFilterSecurityInterceptor centitPowerFilter = new DaoFilterSecurityInterceptor();
centitPowerFilter.setAuthenticationManager(authenticationManager);
centitPowerFilter.setAccessDecisionManager(centitAccessDecisionManagerBean);
centitPowerFilter.setSecurityMetadataSource(centitSecurityMetadataSource);
centitPowerFilter.setSessionRegistry(centitSessionRegistry);
centitPowerFilter.setAllResourceMustBeAudited(
BooleanBaseOpt.castObjectToBoolean(
env.getProperty("access.resource.must.be.audited"),false));
return centitPowerFilter;
}
protected AjaxAuthenticationFailureHandler createAjaxFailureHandler() {
AjaxAuthenticationFailureHandler ajaxFailureHandler = new AjaxAuthenticationFailureHandler();
String defaultTargetUrl = env.getProperty("login.failure.targetUrl");
ajaxFailureHandler.setDefaultFailureUrl(
StringBaseOpt.emptyValue(defaultTargetUrl,
"/system/mainframe/login/error"));
ajaxFailureHandler.setWriteLog(
BooleanBaseOpt.castObjectToBoolean(
env.getProperty("login.failure.writeLog"),false));
return ajaxFailureHandler;
}
protected AjaxAuthenticationSuccessHandler createAjaxSuccessHandler(CentitUserDetailsService centitUserDetailsService) {
AjaxAuthenticationSuccessHandler ajaxSuccessHandler = new AjaxAuthenticationSuccessHandler();
String defaultTargetUrl = env.getProperty("login.success.targetUrl");
ajaxSuccessHandler.setDefaultTargetUrl(StringBaseOpt.emptyValue(defaultTargetUrl,"/"));
ajaxSuccessHandler.setWriteLog(BooleanBaseOpt.castObjectToBoolean(
env.getProperty("login.success.writeLog"),true));
ajaxSuccessHandler.setRegistToken(BooleanBaseOpt.castObjectToBoolean(
env.getProperty("login.success.registToken"),false));
ajaxSuccessHandler.setUserDetailsService(centitUserDetailsService);
return ajaxSuccessHandler;
}
protected DaoAccessDecisionManager createCentitAccessDecisionManager() {
DaoAccessDecisionManager accessDecisionManager = new DaoAccessDecisionManager();
accessDecisionManager.setAllResourceMustBeAudited(
BooleanBaseOpt.castObjectToBoolean(
env.getProperty("access.resource.must.be.audited"),false));
return accessDecisionManager;
}
protected DaoInvocationSecurityMetadataSource createCentitSecurityMetadataSource() {
CentitSecurityMetadata.setIsForbiddenWhenAssigned(
BooleanBaseOpt.castObjectToBoolean(env.getProperty("access.resource.must.be.assigned"), false));
return new DaoInvocationSecurityMetadataSource();
}
protected StrictHttpFirewall httpFirewall() {
StrictHttpFirewall firewall = new StrictHttpFirewall();
firewall.setAllowSemicolon(BooleanBaseOpt.castObjectToBoolean(env.getProperty("http.firewall.allowSemicolon"),true));
return firewall;
}
}