默认情况下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;
}
}
实现用户描述接口:
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());
}
}
通过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 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>
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>
。。。。。
补充UserAuthorityInfo代码:
ABCUserAuthority:
public class ABCUserAuthority implements UserDetails{
private static final long serialVersionUID = -6394802605626145354L;
。。。。。
}
UserAuthorityInfo:
@Repository
public class UserAuthorityInfoImp implements UserAuthorityInfo {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private SessionFactory sf;
public UserDetails getUserDetails(String username)
{
ABCUserAuthority user = new ABCUserAuthority();
return user;
}
}
分享到:
相关推荐
第二章:springsecurity起步 第三章:增强用户体验 第四章:凭证安全存储 第五章:精确的访问控制 第六章:高级配置和扩展 第七章:访问控制列表(ACL) 第八章:对OpenID开放 第九章:LDAP目录服务 第十章:使用...
3. Spring Security 4.2的新特性 27 3.1 Web改进 27 3.2配置改进 28 3.3杂项 28 4.样品和指南(从这里开始) 28 5. Java配置 29 5.1 Hello Web安全Java配置 29 5.1.1 AbstractSecurityWebApplicationInitializer 31 ...
同时支持基于客户端证书和表单登录的认证,详细参考http://blog.csdn.net/xiejx618/article/details/51691809
主要介绍了Spring Security OAuth过期的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
在整本书中一直用作视图模板引擎章节LDAP目录服务记住我的服务使用TLS的客户端证书身份验证打开OAuth2 使用中央身份验证服务的单点登录细粒度的访问控制访问控制列表定制授权会话管理其他Spring安全功能具有OAuth2和...
这个项目是Spring Security配置的实践。 它涵盖以下功能: 基本认证 授权 内存中用户详细信息管理器 JDBC用户详细信息管理器 CORS CSRF 使用自签名证书的HTTPS 如何创建自签名证书 在Java bin文件夹中执行以下...
如果需要更高的安全性,则应获取真实的SSL证书: 此密钥库不受保护,应位于更安全的目录中-对于非测试应用程序,最好完全位于应用程序外部-并具有用户帐户可以访问它的严格权限 访问服务 注意
Spring安全测试 功能科 -自定义登录页面 -记得我 -403和404错误页面 -通过JDBC验证用户和密码 -BCrypt保护密码 -Https支持 -运行时重载权限 -启用Csrf Https支持 要启用https,请执行以下步骤: 本示例假设您使用的...
RESTful API Spring示例一个使用spring 4的RESTful应用程序的简单示例。依赖关系经理Maven的3.2.5如何建造$ cd $ mvn clean package如何通过码头部署将文件springsecurityprimer.war复制到jetty webapps文件夹中。...
在日常校园生活中,我们常常在考试前夕查找复习重点和资料,在各类考证书前寻找一些备考宝典,在考研前咨询相关学校和考研经验,在东西丢失后通过一个平台需求帮助,在找工作时了解企业、往年工作情况以及寻求专业...
Ansible剧本,用于执行安装和设置Apache Fortress Demo的步骤,包括OpenLDAP,Apache Tomcat,MariaDB,生成证书,密钥等。 目标环境的先决条件 安装了OpenSSL的Debian或Redhat Liunx变体计算机。 Java 8 ++ ...
3、增加了https安全登录,在springsecurity配置文件中,去掉相应注释就可以了,同时程序根目录'设计文档'中有默认证书需要您导入JDK库中。 4、增加了系统logo上传功能,这样不用重新发布程序就可以修改logo...
当您想通过Spring Security扩展Firebase的身份验证机制以无缝创建和使用受保护的rest API时,此Spring Boot Starter非常适合此类情况。 组态 请确保全局添加以下环境变量或项目特定的运行配置环境变量GOOGLE_...
OAuth2-CLISpring安全性示例的自述文件 上次更新时间:2015年9月7日 该存储库包含使用以下框架以Java实现的示例OAuth2客户端应用程序: Sprint Boot ... 该服务端点已使用Spring Security OAuth2保护。
阿里云 oss java源码 简介 Seezoon Stack 以快速开发为目的,在开发速度和代码结构上做出一定取啥,无论如何,你将看到非常地道的 ...线程池、证书请求及默认属性约定 Spring Boot 异步线程配置 跨域
将Application类作为 Spring Boot 应用程序(main 方法)启动,并查看测试和幻灯片以了解如何使用src/main/resources的证书登录它/pairs/spring-session : 演示,使用共享 cookie 作为前端 UI 和后端服务之间的...
Spring Security+JWT作为安全框架,对网站和移动端,进行用户认证、角色访问控制;掌握Hybird开发技术在Android和iOS移动客户端中的应用;云服务器配置技术,包括java运行环境安装、Nginx作为web服务器和前后端分离...
认证服务器使用keytool生成的公私密匙工具,主要用于spring-authorization-server的keytool生成,sping,spring-security,oauth2.0,包括生成证书,导出公匙
java期末大作业智慧社区电子出入证信息管理系统源码。二、 需求分析 本实验项目要求团队设计实现的智慧社区电子出入证信息管理系统可以解决如下需求: 1、 本项目可以分为三个客户端: ... b) 使用Spring Security
带有SSL的通用SSL /证书身份验证的自动配置,带有Keycloak JWT服务和规则一个示例SecurityConfiguration @Configuration@EnableWebSecurity@ComponentScan ( basePackageClasses = KeycloakSecurityComponents ....