默認情況下ss3的<x509>標籤只會取證書主題作爲驗證條件,如果想要自己指定證書的某一部分作爲驗證條件需要手動實現X509PrincipalExtractor接口:
- import org.springframework.security.web.authentication.preauth.x509.X509PrincipalExtractor;
- public class MyX509PrincipalExtractor implements X509PrincipalExtractor{
- Logger logger = LoggerFactory.getLogger(this.getClass());
- /**
- * 獲取證書序列號
- * @param cert x509證書對象
- */
- @Override
- public Object extractPrincipal(X509Certificate cert) {
- String serialNumber = cert.getSerialNumber().toString(16);//取證書序列號作爲判斷條件
- return serialNumber;
- }
- }
import org.springframework.security.web.authentication.preauth.x509.X509PrincipalExtractor;
public class MyX509PrincipalExtractor implements X509PrincipalExtractor{
Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 獲取證書序列號
* @param cert x509證書對象
*/
@Override
public Object extractPrincipal(X509Certificate cert) {
String serialNumber = cert.getSerialNumber().toString(16);//取證書序列號作爲判斷條件
return serialNumber;
}
}
實現用戶描述接口:
- public class MyUserAuthority implements UserDetails{
- ……
- }
public class MyUserAuthority implements UserDetails{
……
}
載入用戶信息:
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.security.core.Authentication;
- import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.security.core.userdetails.UsernameNotFoundException;
- /**
- *
- * Company: xxx公司 <br>
- *
- * Description: 用戶信息載入服務
- *
- * <br>Copyright: Copyright (c) 2010 - 2015
- *
- * <br>Author: JLCON
- * <br>Created:2010-9-17
- *
- * <br>Modified:2010-9-17
- *
- * <br>version:V1.0
- */
- public class MyUserDetailService implements AuthenticationUserDetailsService{
- Logger logger = LoggerFactory.getLogger(this.getClass());
- //載入用戶信息
- @Autowired
- private UserAuthorityInfo userinfo;
- /**
- * 用戶信息載入
- * @param token 認證token
- */
- @Override
- public UserDetails loadUserDetails(Authentication token)
- throws UsernameNotFoundException {//這裏得到的就是剛纔返回的證書ID
- return userinfo.getUserDetails(token.getPrincipal().toString());
- }
- }
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.AuthenticationUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
/**
*
* Company: xxx公司 <br>
*
* Description: 用戶信息載入服務
*
* <br>Copyright: Copyright (c) 2010 - 2015
*
* <br>Author: JLCON
* <br>Created:2010-9-17
*
* <br>Modified:2010-9-17
*
* <br>version:V1.0
*/
public class MyUserDetailService implements AuthenticationUserDetailsService{
Logger logger = LoggerFactory.getLogger(this.getClass());
//載入用戶信息
@Autowired
private UserAuthorityInfo userinfo;
/**
* 用戶信息載入
* @param token 認證token
*/
@Override
public UserDetails loadUserDetails(Authentication token)
throws UsernameNotFoundException {//這裏得到的就是剛纔返回的證書ID
return userinfo.getUserDetails(token.getPrincipal().toString());
}
}
通過URL獲取該URL具有的訪問屬性:
- public class X509securityMetadataSource implements FilterInvocationSecurityMetadataSource{
- import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
- …………
- @Override
- public Collection<ConfigAttribute> getAttributes(Object object)
- throws IllegalArgumentException {
- String url = ((FilterInvocation)object).getRequestUrl();
- ………………
- return list;
- }
- @Override
- public boolean supports(Class<?> clazz) {
- return true;
- }
- }
public class X509securityMetadataSource implements FilterInvocationSecurityMetadataSource{
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
…………
@Override
public Collection<ConfigAttribute> getAttributes(Object object)
throws IllegalArgumentException {
String url = ((FilterInvocation)object).getRequestUrl();
………………
return list;
}
@Override
public boolean supports(Class<?> clazz) {
return true;
}
}
認證訪問控制器:
- public class X509AccessDecisionManager implements AccessDecisionManager{
- Logger logger = LoggerFactory.getLogger(this.getClass());
- /**
- * 決定是否有權限訪問資源
- * @param authentication 登錄用戶權限信息
- * @param object 訪問的資源對象
- * @param configAttributes 資源對象具有的配置屬性
- * @exception AccessDeniedException 訪問被拒絕
- */
- @Override
- public void decide(Authentication authentication, Object object,
- Collection<ConfigAttribute> configAttributes)
- throws AccessDeniedException, InsufficientAuthenticationException {
- FilterInvocation filterInvocation = (FilterInvocation)object;
- for(ConfigAttribute configAttribute:configAttributes)
- {
- for(GrantedAuthority grantedAuthority:authentication.getAuthorities())
- {
- if(configAttribute.getAttribute().equalsIgnoreCase(grantedAuthority.getAuthority()))
- {
- logger.debug("訪問success! - {}",filterInvocation.getFullRequestUrl());
- return;
- }
- }
- }
- logger.debug("無權訪問! - {}",filterInvocation.getFullRequestUrl());
- throw new AccessDeniedException("無權限!");
- }
- @Override
- public boolean supports(ConfigAttribute attribute) {
- return true;
- }
- @Override
- public boolean supports(Class<?> clazz) {
- return true;
- }
- }
public class X509AccessDecisionManager implements AccessDecisionManager{
Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 決定是否有權限訪問資源
* @param authentication 登錄用戶權限信息
* @param object 訪問的資源對象
* @param configAttributes 資源對象具有的配置屬性
* @exception AccessDeniedException 訪問被拒絕
*/
@Override
public void decide(Authentication authentication, Object object,
Collection<ConfigAttribute> configAttributes)
throws AccessDeniedException, InsufficientAuthenticationException {
FilterInvocation filterInvocation = (FilterInvocation)object;
for(ConfigAttribute configAttribute:configAttributes)
{
for(GrantedAuthority grantedAuthority:authentication.getAuthorities())
{
if(configAttribute.getAttribute().equalsIgnoreCase(grantedAuthority.getAuthority()))
{
logger.debug("訪問success! - {}",filterInvocation.getFullRequestUrl());
return;
}
}
}
logger.debug("無權訪問! - {}",filterInvocation.getFullRequestUrl());
throw new AccessDeniedException("無權限!");
}
@Override
public boolean supports(ConfigAttribute attribute) {
return true;
}
@Override
public boolean supports(Class<?> clazz) {
return true;
}
}
最後上配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <b:beans xmlns:b="http://www.springframework.org/schema/beans"
- xmlns="http://www.springframework.org/schema/security"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
- <http access-denied-page="/accessdenied.jsp">
- <custom-filter position="X509_FILTER" ref="x509Filter"/>
- <custom-filter ref="x509Intercepter" before="FILTER_SECURITY_INTERCEPTOR"/>
- <intercept-url pattern="/*" requires-channel="https"/>
- <port-mappings>
- <port-mapping http="8080" https="8443"/>
- </port-mappings>
- <form-login/>
- </http>
- <b:bean id="preAuthenticatedProcessingFilterEntryPoint" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedProcessingFilterEntryPoint">
- </b:bean>
- <b:bean id="x509Filter" class="org.springframework.security.web.authentication.preauth.x509.X509AuthenticationFilter">
- <b:property name="authenticationManager" ref="authenticationmanager"></b:property>
- <b:property name="principalExtractor">
- <b:bean class=".....MyX509PrincipalExtractor"></b:bean>
- </b:property>
- </b:bean>
- <b:bean id="x509Intercepter" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
- <b:property name="authenticationManager" ref="authenticationmanager"></b:property>
- <b:property name="securityMetadataSource" ref="x509securityMetadataSource"></b:property>
- <b:property name="accessDecisionManager" ref="x509AccessDecisionManager"></b:property>
- </b:bean>
- <b:bean id="x509securityMetadataSource" class="....X509securityMetadataSource"></b:bean>
- <b:bean id="x509AccessDecisionManager" class="....X509AccessDecisionManager"></b:bean>
- <authentication-manager alias="authenticationmanager" >
- <authentication-provider ref="x509provider">
- </authentication-provider>
- </authentication-manager>
- <b:bean id="x509provider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
- <b:property name="preAuthenticatedUserDetailsService" ref="UserDetailsService">
- </b:property>
- <b:property name="throwExceptionWhenTokenRejected" value="true"></b:property>
- </b:bean>
- <b:bean id="loggerListener" class="org.springframework.security.authentication.event.LoggerListener"/>
- <b:bean id="UserDetailsService" class="....MyUserDetailService"></b:bean>
- <b:bean id="UserAuthorityInfo" class="....UserAuthorityInfoImp"></b:bean>
- </b:beans>
<?xml version="1.0" encoding="UTF-8"?> <b:beans xmlns:b="http://www.springframework.org/schema/beans" xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> <http access-denied-page="/accessdenied.jsp"> <custom-filter position="X509_FILTER" ref="x509Filter"/> <custom-filter ref="x509Intercepter" before="FILTER_SECURITY_INTERCEPTOR"/> <intercept-url pattern="/*" requires-channel="https"/> <port-mappings> <port-mapping http="8080" https="8443"/> </port-mappings> <form-login/> </http> <b:bean id="preAuthenticatedProcessingFilterEntryPoint" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedProcessingFilterEntryPoint"> </b:bean> <b:bean id="x509Filter" class="org.springframework.security.web.authentication.preauth.x509.X509AuthenticationFilter"> <b:property name="authenticationManager" ref="authenticationmanager"></b:property> <b:property name="principalExtractor"> <b:bean class=".....MyX509PrincipalExtractor"></b:bean> </b:property> </b:bean> <b:bean id="x509Intercepter" class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor"> <b:property name="authenticationManager" ref="authenticationmanager"></b:property> <b:property name="securityMetadataSource" ref="x509securityMetadataSource"></b:property> <b:property name="accessDecisionManager" ref="x509AccessDecisionManager"></b:property> </b:bean> <b:bean id="x509securityMetadataSource" class="....X509securityMetadataSource"></b:bean> <b:bean id="x509AccessDecisionManager" class="....X509AccessDecisionManager"></b:bean> <authentication-manager alias="authenticationmanager" > <authentication-provider ref="x509provider"> </authentication-provider> </authentication-manager> <b:bean id="x509provider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider"> <b:property name="preAuthenticatedUserDetailsService" ref="UserDetailsService"> </b:property> <b:property name="throwExceptionWhenTokenRejected" value="true"></b:property> </b:bean> <b:bean id="loggerListener" class="org.springframework.security.authentication.event.LoggerListener"/> <b:bean id="UserDetailsService" class="....MyUserDetailService"></b:bean> <b:bean id="UserAuthorityInfo" class="....UserAuthorityInfoImp"></b:bean> </b:beans>
web.xml
- 。。。。。
- <filter>
- <filter-name>springSecurityFilterChain</filter-name>
- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>springSecurityFilterChain</filter-name>
- <url-pattern>/manager/*</url-pattern>
- </filter-mapping>
- 。。。。。