http://snz.iteye.com/blog/221280
由于ss2的demo配置太过简单,要想在项目中应用的话必须进行相应扩展,这里简单写一下简单的扩展方法。
xml头中引入security命名空间
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:security="http://www.springframework.org/schema/security"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd"
- default-lazy-init="true">
然后是启用ss2默认配置的一段代码
- <!--
- 2.0新增的命名空间,使得配置简化了很多
- auto-config 自动使用默认的配置
- access-denied-page 指定访问未授权页面时显示的页面
- -->
- <security:http auto-config="true" access-denied-page="/accessDenied.html">
- <security:anonymous granted-authority="BASIC" />
- </security:http>
这段代码作用是以ss2的默认配置方式加入1.0时需要手工配置的AuthenticationProcessingFilter等多个必须配置的filter,详细可参考1.0配置和2.0参考手册。
auto-config="true" 表示使用ss2自动配置
access-denied-page="/accessDenied.html"表示拒绝访问时显示的页面
<security:anonymous granted-authority="BASIC" />表示匿名权限的authority为BASIC
ss对权限的管理分为认证和授权两部分,先看认证
-
-
-
- <bean id="authenticationProcessingFilter"
- class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter">
-
- <security:custom-filter before="AUTHENTICATION_PROCESSING_FILTER" />
-
- <property name="authenticationManager" ref="authenticationManager" />
-
- <property name="authenticationFailureUrl"
- value="/spring_security_login" />
-
- <property name="defaultTargetUrl" value="/index.html" />
- </bean>
这是负责认证处理的filter,中间custom-filter一行意思是将filter放在默认配置中别名为AUTHENTICATION_PROCESSING_FILTER的filter前边,即负责认证的filter(别名列表参照参考手册)。
按官方的说法,如果需要用自定义的filter覆盖默认filter,则应该将security:http标签的auto-config属性改为false,这样的话就需要增加很多手动配置项。我试了下,不改false也可以,只是运行期间会出现一个warn信息“Possible error: Filters at position 2 and 3 are both instances of xxxx”,意思是filter串中有两个相同类型的filter。
另:在2.0.2中可以使用position代替before,真正的覆盖默认filter。但是有个bug,如果使用默认登录入口的话,还是会调用默认filter,必须连登录入口一并改掉。
其引用的authenticationManager
- <!--
- 认证管理器
- 根据用户名和密码,使用多个provider进行认证
- 认证成功会生成一个Authentication,否则抛出AuthenticationException
- -->
- <bean id="authenticationManager"
- class="org.springframework.security.providers.ProviderManager">
- <property name="providers">
- <list>
- <ref local="daoAuthenticationProvider" />
- </list>
- </property>
- </bean>
认证管理器通过多个provider实现基于用户名和密码的认证,多个provider中只要有一个认证成功,即成功。
这里只使用了一个daoPorvider
- <!--
- 认证的provider
- userDetailsService 根据用户名获取用户信息
- userCache ehcache缓存user信息。
- -->
- <bean id="daoAuthenticationProvider"
- class="org.springframework.security.providers.dao.DaoAuthenticationProvider">
- <property name="userDetailsService" ref="userDetailsService" />
- <property name="userCache" ref="userCache" />
- </bean>
userDetailsService:根据登录的用户名获取一个UserDetails,即代表一个用户的实体对象。
-
- <bean id="userDetailsService"
- class="org.catspaw.ss2test1.security.UserDetailsSerivceImpl">
- <property name="userDao" ref="userDao" />
- </bean>
UserDetailsSerivceImpl代码
- package org.catspaw.ss2test1.security;
-
- import org.catspaw.ss2test1.dao.UserDao;
- import org.springframework.dao.DataAccessException;
- import org.springframework.security.userdetails.UserDetails;
- import org.springframework.security.userdetails.UserDetailsService;
- import org.springframework.security.userdetails.UsernameNotFoundException;
-
-
-
-
-
-
-
-
- public class UserDetailsSerivceImpl implements UserDetailsService {
-
- private UserDao userDao;
-
- public UserDao getUserDao() {
- return userDao;
- }
-
- public void setUserDao(UserDao userDao) {
- <span cla
分享到:
相关推荐
终于实现了spring security 2.0 基于数据库的配置,可以连接数据库了,呵呵,由于加入了spring,jar包有些大,没有上传jar,或者新建一个工程,加入spring,然后将jar考过来或者从其他模块中考入jar, 这个工程下的...
spring cloud2.0 eureka server spring security配置,与spring cloud1.X还是有很大区别
通过此PPT可进行Acegi的配置,最新Spring security2.0的安全构建JAVA系统
博文链接:https://snz.iteye.com/blog/229915
从Spring-2.0开始可以使用命名空间的配置方式。 使用它呢,可以通过附加xml架构,为传统的spring beans应用环境语法做补充。 你可以在spring参考文档得到更多信息。 命名空间元素可以简单的配置单个bean,或使用更...
NULL 博文链接:https://lym6520.iteye.com/blog/309169
Spring Security 为基于J2EE 的企业应用软件提供了一套全面的安全解决方案。正如你在本手册中看到的 那样,我们尝试为您提供一套好用,高可配置的安全系统。 安全问题是一个不断变化的目标,更重要的是寻求一种全面...
Spring Security框架 oAuth2.0协议标准,实现认证服务器和资源服务器,并实现oAuth2.0自定义登陆和授权界面,Access_token和Refresh_token过期时间的设置,以及数据库表结构建表语句,参考博客能更好的学习和了解,...
springboot、spring security、Oauth2.0的授权配置代码 1、项目架构 springboot、spring security、Oauth2.0 2、主要功能 (1)允许客户端token调用 (2)允许客户端token调用 (3)数据库存储令牌
Spring Security 2.0 是Spring框架的下一代安全系统。它在上一代Acegi安全系统上又添加了许多新特性。本文件对Spring Security运行一个简单的应用程序和配置进行详细的说明。
OAuth 2.0 客户端模式的例子,client_id保存在数据库,而不是写死在配置文件中,支持多个client_id
权限安全管理(Spring_security_2.0),详细配置文档,适用于新手入门
Spring Security 中可以使用Acegi-1.x 时代的普通配置方式,也可以使用 从2.0 时代才出现的命名空间配置方式,实际上这两者实现的功能是完 全一致的,只是新的命名空间配置方式可以把原来需要几百行的配置压 缩成短...
Spring Security OAuth2.0 MySQL ##修改数据库配置 修改defender-oauth2-authorization\src\main\resources\application.properties中MySQL的主机配置 修改defender-oauth2-resource\src\main\...
使用Java注释(无XML)已完全定义了配置。 SSOCircle ( )用作测试的公共身份提供者。 作者: Vincenzo De Notaris( ) 网站: 版本: 2.3.1.RELEASE 最后更新:2020年2月15日感谢VladimírSchäfer ( )支持我...
Spring Security OAuth 2.0指南本示例显示了如何使用Spring Security和OAuth 2.0创建授权服务器。 请阅读获取指导教程,向您展示如何在此存储库中构建应用程序。 先决条件: 。 具有身份验证和用户管理API,可通过...
我在配置spring security2.0是的工作笔记 很有启发性
使用spring oauth2框架做授权鉴定。想获取当前用户信息怎么办? 我们知道spring oauth2是基于spring security的实现的。 spring security可以通过SecurityContextHolder.getContext().getAuthentication()....
配置拦截器 文件上传下载 异步请求开发 Restful API 开发常用辅助框架 生成服务文档 mock : 伪造服务 GET /user (用户分页查询)(Pageable封装) 先写好测试类,指定期望的响应码 200 和 期望得到的数据长度 3 ...