- 浏览: 249951 次
- 性别:
- 来自: 苏州
文章分类
- 全部博客 (289)
- java (72)
- oracle (3)
- mysql (5)
- spring (28)
- hibernate (2)
- osgi (0)
- linux (2)
- ExtJs (1)
- jvm (0)
- mybatis (7)
- 分布式 (11)
- MINA (6)
- apache+tomcat (13)
- js+htm (7)
- android (44)
- http (1)
- hbase+hdoop (0)
- memcache (13)
- search (27)
- 部署及性能 (12)
- mongoDB (2)
- 多线程 (12)
- 安全管理验证 (9)
- struts (1)
- webservice (0)
- easyUI (1)
- spring security (16)
- pattern (6)
- 算法 (2)
最新评论
-
lzh8189146:
CommonsHttpSolrServer这个类,现在是不是没 ...
CommonsHttpSolrServer -
xiaochanzi:
我按照你的方法试了下,tomcat6可以发布,但是访问任何网页 ...
基于内嵌Tomcat的应用开发 -
phoneeye:
麻烦你,如果是抄来的文章,请给出来源。谢谢
ant 两则技巧 -
neverforget:
转载不注明出处
Spring Security3.1登陆验证 替换 usernamepasswordfilter -
liang1022:
若不使用eclipse ,如何在命令行下 运行服务端程序 ?
WebService CXF学习(入门篇2):HelloWorld
先在web.xml 中配置一个过滤器(必须在Struts的过滤器之前)
- <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>/*</url-pattern>
- </filter-mapping>
然后就是编写Spring安全的配置文件applicationContext-security.xml并配置到Spring解析的路径下
Spring Security主要做两件事,一件是认证,一件是授权。
认证
当用户访问受保护的信息却没有登录获得认证时,框架会自动将请求跳转到登录页面在http标签中的
- <form-login login-page="/page/login.jsp" />
配置。且该登录页面必须是不被拦截的。故要配置上
- <intercept-url pattern="/page/login.jsp" filters="none" />
Web项目的认证如果在HTTP标签中配置了auto-config="true",框架就会自动的配置多8?个拦截器。 默认表单登录认证的是FORM_LOGIN_FILTER拦截器,我们可以直接写自定义的UserDetailsService,在这个类中实现方法UserDetails loadUserByUsername(String username),从数据库获取用户信息,以及其拥有的角色。
- @Service("myUserDetailsService")
- public class MyUserDetailsServiceImpl extends BaseService implements UserDetailsService {
- @Resource
- private UserDao userDao;
- public UserDetails loadUserByUsername(String username)
- throws UsernameNotFoundException, DataAccessException {
- User user = userDao.getUserByUsername(username);
- List<Role> roles = user.getRoles();
- Collection<GrantedAuthority> authorities = new LinkedList<GrantedAuthority>();
- for (Role role : roles) {
- authorities.add(new GrantedAuthorityImpl(role.getCode()));
- }
- UserDetails userDetails = new org.springframework.security.core.userdetails.User(username,user.getPassword(),Constants.STATE_VALID.equals(user.getState()),true,true,true,authorities);
- return userDetails;
- }
- }
配置在
- <authentication-manager alias="myAuthenticationManager">
- <authentication-provider user-service-ref="myUserDetailsService">
- <password-encoder hash="md5" />
- </authentication-provider>
- </authentication-manager>
如果需要在登录的时候,在HTTP SESSION中配置做些操作的。就得配置自定义的FORM_LOGIN_FILTER了
在HTTP标签中加入
- <custom-filter ref="loginFilter" before="FORM_LOGIN_FILTER" />
并配置
- <!-- 访问控制验证器Authority -->
- <beans:bean id="securityFilter"
- class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
- <beans:property name="authenticationManager" ref="myAuthenticationManager" />
- <beans:property name="accessDecisionManager"
- ref="affirmativeBasedAccessDecisionManager" />
- <beans:property name="securityMetadataSource" ref="mySecurityMetadataSource"/>
- </beans:bean>
MyUsernamePasswordAuthenticationFilter 类是这么写的
- public class MyUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter{
- public static final String USERNAME = "username";
- public static final String PASSWORD = "password";
- @Resource
- private LoginService loginService;
- private UserLoginFormBean userLoginFormBean = new UserLoginFormBean();
- @Resource
- private LogService logService;
- @Override
- public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
- String username = obtainUsername(request);
- String password = obtainPassword(request);
- HttpSession session = request.getSession();
- userLoginFormBean.setUsername(obtainUsername(request));
- userLoginFormBean.setPassword(obtainPassword(request));
- User user = loginService.login(userLoginFormBean);
- session.setAttribute(Constants.SESSION_USER, user);
- Log log = new Log(user,getIpAddr(request),"用户登录", null);
- logService.add(log);
- //UsernamePasswordAuthenticationToken实现 Authentication
- UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
- // Place the last username attempted into HttpSession for views
- // 允许子类设置详细属性
- setDetails(request, authRequest);
- // 运行UserDetailsService的loadUserByUsername 再次封装Authentication
- return this.getAuthenticationManager().authenticate(authRequest);
- }
- }
getAuthenticationManager().authenticate(authRequest)是为了让UserDetailService提供Detailed的信息并认证
授权
在授权时,系统默认通过FILTER_SECURITY_INTERCEPTOR认证。
如需自定义授权拦截器,我们在HTTP中在默认授权拦截器前配置了自定义的拦截器
- <custom-filter ref="securityFilter" before="FILTER_SECURITY_INTERCEPTOR" />
本平台采用基于请求URL地址的验证方式
securityFilter的配置如下
- <!-- 访问控制验证器Authority -->
- <beans:bean id="securityFilter"
- class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
- <beans:property name="authenticationManager" ref="myAuthenticationManager" />
- <beans:property name="accessDecisionManager"
- ref="affirmativeBasedAccessDecisionManager" />
- <beans:property name="securityMetadataSource" ref="mySecurityMetadataSource"/>
- </beans:bean>
采用默认的自定义的也是采用Spring默认的FilterSecurityInterceptor拦截器,accessDecisionManager也采用的是框架提供的affirmativeBasedAccessDecisionManager
采用投票者来判断是否授权。
- <beans:bean id="affirmativeBasedAccessDecisionManager"
- class="org.springframework.security.access.vote.AffirmativeBased">
- <beans:property name="decisionVoters" ref="roleDecisionVoter" />
- </beans:bean>
- <beans:bean name="roleDecisionVoter"
- class="org.springframework.security.access.vote.RoleVoter" />
- <beans:bean id="mySecurityMetadataSource"
- class="org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource">
- <beans:constructor-arg
- type="org.springframework.security.web.util.UrlMatcher" ref="antUrlPathMatcher" />
- <beans:constructor-arg type="java.util.LinkedHashMap"
- ref="securityRequestMapFactoryBean" />
- </beans:bean>
SecurityMetadataSource也是ss web框架提供的DefaultFilterInvocationSecurityMetadataSource,只是初始化参数中,一个选择antUrl匹配,还是正则匹配,另一个是提供自定义的通过securityRequestMapFactoryBean。在后者是一个LinkedHashMap<RequestKey, Collection<ConfigAttribute>>类型,就是每一个URL匹配模式,所需要角色的集合。
- @Service("securityRequestMapFactoryBean")
- public class SecurityRequestMapFactoryBean extends
- LinkedHashMap<RequestKey, Collection<ConfigAttribute>> {
- @Resource
- private ModuleDao moduleDao;
- @PostConstruct
- public void loadSecurityInfos(){
- List<Module> modules = moduleDao.getAll(new Module());
- // List<Role> roles = roleDao.getAll(new Role());
- for (Module module : modules) {
- RequestKey requestKey = new RequestKey(module.getPageUrl());
- Collection<ConfigAttribute> configAttributes = new LinkedList<ConfigAttribute>();
- for (final Role role : module.getRoles()) {
- configAttributes.add(new ConfigAttribute() {
- public String getAttribute() {
- return role.getCode();
- }
- });
- }
- this.put(requestKey, configAttributes);
- }
- }
- }
PS: 最终的件applicationContext-security.xml配置文件
- <pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?>
- <beans:beans xmlns="http://www.springframework.org/schema/security"
- xmlns:beans="http://www.springframework.org/schema/beans" 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 auto-config="true">
- <intercept-url pattern="/page/login.jsp" filters="none" />
- <intercept-url pattern="/LoginAction*" filters="none" />
- <intercept-url pattern="/common/**" filters="none" />
- <intercept-url pattern="/css/**" filters="none" />
- <intercept-url pattern="/common/**" filters="none" />
- <intercept-url pattern="/images/**" filters="none" />
- <intercept-url pattern="/js/**" filters="none" />
- <form-login login-page="/page/login.jsp" />
- <custom-filter ref="loginFilter" before="FORM_LOGIN_FILTER" />
- <custom-filter ref="securityFilter" before="FILTER_SECURITY_INTERCEPTOR" />
- </http>
- <!-- 访问控制验证器Authority -->
- <beans:bean id="securityFilter"
- class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
- <beans:property name="authenticationManager" ref="myAuthenticationManager" />
- <beans:property name="accessDecisionManager"
- ref="affirmativeBasedAccessDecisionManager" />
- <beans:property name="securityMetadataSource" ref="mySecurityMetadataSource"/>
- </beans:bean>
- <!-- 登录验证器Authentication -->
- <beans:bean id="loginFilter"
- class="com.epro.crm.util.security.MyUsernamePasswordAuthenticationFilter">
- <!-- 处理登录的action -->
- <beans:property name="filterProcessesUrl" value="/SecurityCheck" />
- <!-- 验证成功后的处理-->
- <beans:property name="authenticationSuccessHandler"
- ref="loginLogAuthenticationSuccessHandler" />
- <!-- 验证失败后的处理-->
- <beans:property name="authenticationFailureHandler"
- ref="simpleUrlAuthenticationFailureHandler" />
- <beans:property name="authenticationManager" ref="myAuthenticationManager" />
- <!-- 注入DAO为了查询相应的用户 -->
- <beans:property name="loginService" ref="loginService" />
- <beans:property name="logService" ref="logService" />
- </beans:bean>
- <authentication-manager alias="myAuthenticationManager">
- <authentication-provider user-service-ref="myUserDetailsService">
- <password-encoder hash="md5" />
- </authentication-provider>
- </authentication-manager>
- <beans:bean id="mySecurityMetadataSource"
- class="org.springframework.security.web.access.intercept.DefaultFilterInvocationSecurityMetadataSource">
- <beans:constructor-arg
- type="org.springframework.security.web.util.UrlMatcher" ref="antUrlPathMatcher" />
- <beans:constructor-arg type="java.util.LinkedHashMap"
- ref="securityRequestMapFactoryBean" />
- </beans:bean>
- <beans:bean id="antUrlPathMatcher"
- class="org.springframework.security.web.util.AntUrlPathMatcher" />
- <beans:bean id="affirmativeBasedAccessDecisionManager"
- class="org.springframework.security.access.vote.AffirmativeBased">
- <beans:property name="decisionVoters" ref="roleDecisionVoter" />
- </beans:bean>
- <beans:bean name="roleDecisionVoter"
- class="org.springframework.security.access.vote.RoleVoter" />
- <beans:bean id="loginLogAuthenticationSuccessHandler"
- class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
- <beans:property name="defaultTargetUrl" value="/page/main.jsp"></beans:property>
- </beans:bean>
- <beans:bean id="simpleUrlAuthenticationFailureHandler"
- class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
- <!--
- 可以配置相应的跳转方式。属性forwardToDestination为true采用forward false为sendRedirect
- -->
- <beans:property name="defaultFailureUrl" value="/page/login.jsp"></beans:property>
- </beans:bean>
- <!-- 未登录的切入点 -->
- <beans:bean id="authenticationProcessingFilterEntryPoint"
- class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
- <beans:property name="loginFormUrl" value="/page/login.jsp"></beans:property>
- </beans:bean>
- </beans:beans>
------------------------------------------------------ 分割线 -----------------------------------------------------------------
后记: 由于权限配置信息,是由初始化mySecurityMetadataSource时,就由mySecurityMetadataSource读取提供的权限信息,并缓存与该类的私有成员变量中,所以重新加载时就需要重新新建一个对象
- public void loadSecurityInfos(){
- this.clear();
- List<Module> modules = moduleDao.getAll(new Module());
- Collections.sort(modules);
- for (Module module : modules) {
- RequestKey requestKey = new RequestKey(module.getPageUrl());
- Collection<ConfigAttribute> configAttributes = new LinkedList<ConfigAttribute>();
- moduleDao.refresh(module);
- List<Role> roles = module.getRoles();
- if(roles != null){
- for (final Role role : roles) {
- configAttributes.add(new ConfigAttribute() {
- public String getAttribute() {
- return role.getCode();
- }
- });
- }
- }
- this.put(requestKey, configAttributes);
- log.info(module.getName()+ "模块 URL模式:" + requestKey + " 授权角色:"+ roles);
- }
- }
发表评论
-
在spring security3上实现验证码
2012-04-21 16:07 613在spring security3上实现验证码 h ... -
Spring Security3.1登陆验证 替换 usernamepasswordfilter
2012-04-15 02:01 3369一、前言 在上一篇http://blog.c ... -
Spring Security 3应用的11个步骤
2012-04-15 01:47 1128Spring Security 3应用的11个步骤 ... -
在spring security3上实现验证码
2012-03-01 08:44 600转载: 在spring securi ... -
spring security 密码编码器
2012-03-14 08:13 931spring security 密码编码器 (2 ... -
spring security 配置详解
2012-02-25 11:50 2137http://static.springsource.org/ ... -
图解Spring Security默认使用的过滤器
2012-02-24 08:13 738转载:http://www.flyysoft.com/plus ... -
Spring Security 3 与 CAS单点登录配置-Server
2012-03-15 19:56 1257转载:http://www.hxdw.com/bbs/post ... -
Topic: Spring Security 3 与 CAS单点登录配置-Client
2012-02-26 12:12 791转载: http://www.hxdw.com ... -
Spring Security 3 基于角色访问控制过程详解
2012-02-19 13:25 1373访问控制:由于我们配 ... -
修改spring security源码实现动态授权
2012-02-21 08:15 885修改spring security源码实现动态 ... -
spring security 3.0 logout filter 代码中的一个小bug
2012-03-15 19:57 962spring security 3.0 log ... -
spring security 源码分析: 过滤器
2012-03-19 08:23 980spring security 源码分析: 过 ... -
spring security遇到的一些问题
2012-03-19 08:24 635spring security遇到的一些问题 ... -
spring security 源码解读 1
2012-03-19 08:24 835http://feiyan35488.iteye.com/bl ...
相关推荐
本项目基于Spring,整合Spring的security模块,实现用户管理和权限控制,是一套较为通用的权限控制功能,主要内容如下: 1.登录,包括“记住我”的功能; 2.加密,存储的密码不采用明文,初始密码1234; 3.拦截器...
Java企业级权限系统,可供学习Spring Security权限框架、 Apache Shiro权限框架、Spring MVC、 RBAC模型、模块开发等等,内有详细视频教程,由于上传大小限制,可联系免费获取!
它由Sun Microsystems(现在是Oracle Corporation)的James Gosling等人在1995年推出,被设计为一种简单、健壮、可移植、多线程、动态的语言。Java的主要特点和优势包括以下几个方面: 跨平台性(Write Once, Run ...
eladmin,项目基于 Spring Boot 2.1.0 、 Jpa、 Spring Security、redis、Vue 的前后端分离的后台管理系统,项目采用分模块开发方式, 权限控制采用 RBAC,支持数据字典与数据权限管理,前端菜单支持动态路由.zip
基于RBAC模型构建权限管理模块,并集成安全框架SpringSecurity,实现用户的认证和授权。 使用Spring Data集成缓存中间件Redis,加快访问速度。 使用Spring Data集成全文搜索搜索引擎ElasticSearch,实现文章信息的...
Security开发的权限管理模块。 一、RBAC模型介绍 RBAC是Role-Based Access Control的缩写,意思就是基于角色的权限访问控制。 基本思想: 对系统的各种权限不是直接授予具体的用户,而是在用户集合与权限集合之间...
一个基于Spring Boot 2.1.0,Spring Boot Jpa,JWT,Spring Security,Redis,Vue的前分离分离的后台管理系统 开发文档: : 体验地址: ://el-admin.xin 账号密码: admin / 123456 活动福利 七牛云2021 ECUG Con...
Spring Security,Mybatis的RBAC权限管理系统基于数据驱动视图的理念封装Element-ui,即使没有vue的使用经验也能快速上手微服务版本核心依赖依赖版本Sprint Boot2.4.3 Mybatis Plus 3.4.2 胡托5.5.8模块说明albedo...
基于RBAC模型构建权限管理模块,并集成安全框架SpringSecurity,实现用户的认证和授权。 使用Spring Data集成缓存中间件Redis,加快访问速度。 使用Spring Data集成全文搜索搜索引擎ElasticSearch,实现文章信息的...
Spring Boot Jpa、 JWT、Spring Security、Redis、Vue的前后端分离的后台管理系统,项目采用分模块开发方式, 权限控制采用 RBAC,支持数据字典与数据权限管理,支持一键生成前后端代码,支持动态路由! EL ADMIN...
基于RBAC模型构建权限管理模块,并集成安全框架SpringSecurity,实现用户的认证和授权。 使用Spring Data集成缓存中间件Redis,加快访问速度。 使用Spring Data集成全文搜索搜索引擎ElasticSearch,实现文章信息的...
Security,该框架提供了一整套比较成熟,也很完整的机制用于处理各类场景下的可以基于权限,资源路径,以及授权方面的解决方案,部分模块支持定制化,而且在和oauth2.0进行了很好的无缝连接,在移动互联网的授权认证...
8- IDE:IntelliJ IDEA- 依赖管理:Maven- 数据库:MySQL 5.7运行项目- 本项目用到 redis,没安装请自行百度安装- 直接运行 SkAdmin.java 即可启动后台服务功能模块- 系统管理 - 用户管理 提供用户的相关
1137679743SPARK 开发平台 作者:polaris.wang如果您喜欢,请伸出您的小手,给作者点个赞,感谢您的支持 :grinning_face_with_smiling_eyes:基于 Spring Cloud Hoxton 、Spring Boot 2.2、 OAuth2 的RBAC权限管理...
整体采用RBAC(基于角色的访问控制,基于角色的访问控制),具有严格的权限控制模块,支持系统与模块分离开发。最后希望这个项目能够对你有所帮助。 Oryx开发交流群: (QQ群)环境版本1.8 5.73.59.0依赖版本2.1.7。...
项目采用分模块开发方式, 权限控制采用 RBAC(Role-Based Access Control,基于角色的访问控制),支持数据字典、数据权限管理、前端菜单支持动态路由,另外还有其他的功能模块:日志管理、代码生成器、系统监控、...
Security,该框架提供了一整套比较成熟,也很完整的机制用于处理各类场景下的可以基于权限,资源路径,以及授权方面的解决方案,部分模块支持定制化,而且在和oauth2.0进行了很好的无缝连接,在移动互联网的授权认证...
eladmin jpa 版本:项目基于 Spring Boot 2.6.4、 Jpa、 Spring Security、Redis、Vue的前后端分离的后台管理系统,项目采用分模块开发方式, 权限控制采用 RBAC,支持数据字典与数据权限管理,支持一键生成前后端...