默认情况下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{ …… }
载入用户信息:
- 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>
- 。。。。。
评论
1 楼
wise_wei
2012-10-15
收_藏_了,请问一下:
//载入用户信息
@Autowired
private UserAuthorityInfo userinfo;
这个 UserAuthorityInfo类是什么?
//载入用户信息
@Autowired
private UserAuthorityInfo userinfo;
这个 UserAuthorityInfo类是什么?
相关推荐
3. **过滤器链**:Spring Security的核心是其过滤器链,包括登录过滤器、CSRF保护、会话管理等。这些过滤器按照特定顺序执行,确保每个请求都经过安全检查。 4. **会话管理**:Spring Security可以实现会话固定保护...
在"springsecurity学习笔记"中,你可能会涉及以下主题: - Spring Security的基本配置,包括web安全配置和全局安全配置。 - 如何自定义认证和授权流程,比如实现自定义的AuthenticationProvider和...
3. **过滤器链(Filter Chain)**:Spring Security的核心是其过滤器链,它在HTTP请求被处理之前进行拦截。过滤器链包含多个预定义的过滤器,如`HttpServletRequestWrapperFilter`、`AnonymousAuthenticationFilter`...
Spring Security是Spring生态体系中的一个核心组件,主要负责应用程序的安全性,包括认证、授权和访问控制。本话题将深入探讨Spring Security的3.0.0和3.1.3两个版本,这两个版本都是该框架历史上的重要里程碑。 ...
### Spring Security3 相关知识点概述 #### 第一章:一个不安全应用的剖析 **安全审计** - **目的**:识别系统中的安全隐患,并评估安全措施的有效性。 - **过程**:通过模拟攻击来测试系统的安全性,分析日志记录...
标题中的"Spring Security 3 与 CAS 单点登录配置-Server"涉及到的是在Java Web开发中使用Spring Security 3框架集成Central Authentication Service (CAS)实现单点登录(Single Sign-On, SSO)的服务器端配置。...
- `spring-security-x509` 演示了如何处理基于X.509证书的SSL客户端认证,这在需要高安全性或企业级应用中常见。 6. **基于LDAP的认证**: - `spring-security-mvc-ldap` 示例可能展示了如何与 Lightweight ...
- **CAS - spring-security-cas-client.jar**:支持CAS单点登录。 - **OpenID - spring-security-openid.jar**:支持OpenID认证。 #### 二、Security命名空间配置 - **命名空间设计**:Spring Security引入了...
在Spring Security 3.x版本中,它引入了许多改进和新特性,以增强应用程序的安全性。这个实例集提供了深入理解如何配置和使用Spring Security 3.x的实践指导。 1. **身份验证**:Spring Security的核心功能之一是...
第二章:springsecurity起步 第三章:增强用户体验 第四章:凭证安全存储 第五章:精确的访问控制 第六章:高级配置和扩展 第七章:访问控制列表(ACL) 第八章:对OpenID开放 第九章:LDAP目录服务 第十章:使用...
### Spring Security 3.0 入门与核心概念详解 #### 一、Spring Security简介 Spring Security 是一款功能强大且高度可定制的应用安全框架,它为基于 Java 的应用程序提供了全面的安全服务。尤其对于那些采用 ...
**Spring Security 2 中文参考文档** Spring Security是一款强大的安全框架,主要用于Java应用程序的安全管理。在Spring Security 2版本中,它提供了丰富的功能,包括身份验证、授权、访问控制以及安全相关的会话...
本使用手册详细介绍了Spring Security的配置和使用方法,包括使用命名空间配置的方式,实现用户权限的管理,自定义登陆页面,以及一些高级功能,如单点登录、防御会话伪造等。 在配置方面,Spring Security支持传统...
2. **OAuth2集成**:Spring Security支持OAuth2协议,可以用于实现第三方登录(如Facebook、Google)。 3. **CSRF防护**:Spring Security提供跨站请求伪造(CSRF)防护机制,防止恶意攻击。 总的来说,Spring ...
7. **OAuth2 and OpenID Connect Support**:SpringSecurity还支持OAuth2和OpenID Connect协议,这对于构建现代的API和单点登录(SSO)系统非常有用。 在"SpringSecurity权限管理开发手册.pdf"和"SpringSecurity...
在"Spring Security双模认证"中,我们关注的是同时支持基于客户端证书和表单登录的两种认证方式。 1. **客户端证书认证**: 客户端证书认证是一种安全的网络身份验证方法,它使用X.509数字证书来确认用户身份。在...
**Spring Security 3 框架详解** Spring Security 是一个强大的和高度可定制的身份验证和访问控制框架,广泛应用于企业级 Java 应用程序中。它提供了全面的安全解决方案,包括认证、授权、会话管理以及跨站请求伪造...