随笔-193  评论-715  文章-1  trackbacks-0
本Blog所有内容不得随意转载,版权属于作者所有。如需转载请与作者联系( fastzch@163.com    QQ:9184314)。
未经许可的转载,本人保留一切法律权益。


Spring Security 3.x 出来一段时间了,跟Acegi是大不同了,与2.x的版本也有一些小小的区别,网上有一些文档,也有人翻译Spring Security 3.x的guide,但通过阅读guide,无法马上就能很容易的实现一个完整的实例。

我花了点儿时间,根据以前的实战经验,整理了一份完整的入门教程,供需要的朋友们参考。
1,建一个web project,并导入所有需要的lib,这步就不多讲了。
2,配置web.xml,使用Spring的机制装载:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
    
<context-param>
        
<param-name>contextConfigLocation</param-name>
        
<param-value>classpath:applicationContext*.xml</param-value>
    
</context-param>

    
<listener>
        
<listener-class>
            org.springframework.web.context.ContextLoaderListener
        
</listener-class>
    
</listener>

    
<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>


    
<welcome-file-list>
        
<welcome-file>login.jsp</welcome-file>
    
</welcome-file-list>
</web-app>
这个文件中的内容我相信大家都很熟悉了,不再多说了。

2,来看看applicationContext-security.xml这个配置文件,关于Spring Security的配置均在其中:
<?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 access-denied-page="/403.jsp"><!-- 当访问被拒绝时,会转到403.jsp -->
        
<intercept-url pattern="/login.jsp" filters="none" />
        
<form-login login-page="/login.jsp"
            authentication-failure-url
="/login.jsp?error=true"
            default-target-url
="/index.jsp" />
        
<logout logout-success-url="/login.jsp" />
        
<http-basic />
        
<!-- 增加一个filter,这点与Acegi是不一样的,不能修改默认的filter了,这个filter位于FILTER_SECURITY_INTERCEPTOR之前 -->
        
<custom-filter before="FILTER_SECURITY_INTERCEPTOR"
            ref
="myFilter" />
    
</http>

    
<!-- 一个自定义的filter,必须包含authenticationManager,accessDecisionManager,securityMetadataSource三个属性,
    我们的所有控制将在这三个类中实现,解释详见具体配置 
-->
    
<beans:bean id="myFilter" class="com.robin.erp.fwk.security.MyFilterSecurityInterceptor">
        
<beans:property name="authenticationManager"
            ref
="authenticationManager" />
        
<beans:property name="accessDecisionManager"
            ref
="myAccessDecisionManagerBean" />
        
<beans:property name="securityMetadataSource"
            ref
="securityMetadataSource" />
    
</beans:bean>
    
    
<!-- 认证管理器,实现用户认证的入口,主要实现UserDetailsService接口即可 -->
    
<authentication-manager alias="authenticationManager">
        
<authentication-provider
            
user-service-ref="myUserDetailService">
            
<!--   如果用户的密码采用加密的话,可以加点“盐”
                <password-encoder hash="md5" />
            
-->
        
</authentication-provider>
    
</authentication-manager>
    
<beans:bean id="myUserDetailService"
        class
="com.robin.erp.fwk.security.MyUserDetailService" />

    
<!-- 访问决策器,决定某个用户具有的角色,是否有足够的权限去访问某个资源 -->
    
<beans:bean id="myAccessDecisionManagerBean"
        class
="com.robin.erp.fwk.security.MyAccessDecisionManager">
    
</beans:bean>
    
    
<!-- 资源源数据定义,即定义某一资源可以被哪些角色访问 -->
    
<beans:bean id="securityMetadataSource"
        class
="com.robin.erp.fwk.security.MyInvocationSecurityMetadataSource" />

</beans:beans>

3,来看看自定义filter的实现:
package com.robin.erp.fwk.security;
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.security.access.SecurityMetadataSource;
import org.springframework.security.access.intercept.AbstractSecurityInterceptor;
import org.springframework.security.access.intercept.InterceptorStatusToken;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;

public class MyFilterSecurityInterceptor extends AbstractSecurityInterceptor
        
implements Filter {

    
private FilterInvocationSecurityMetadataSource securityMetadataSource;

    
// ~ Methods
    
// ========================================================================================================

    
/**
     * Method that is actually called by the filter chain. Simply delegates to
     * the {
@link #invoke(FilterInvocation)} method.
     * 
     * 
@param request
     *            the servlet request
     * 
@param response
     *            the servlet response
     * 
@param chain
     *            the filter chain
     * 
     * 
@throws IOException
     *             if the filter chain fails
     * 
@throws ServletException
     *             if the filter chain fails
     
*/

    
public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) 
throws IOException, ServletException {
        FilterInvocation fi 
= new FilterInvocation(request, response, chain);
        invoke(fi);
    }


    
public FilterInvocationSecurityMetadataSource getSecurityMetadataSource() {
        
return this.securityMetadataSource;
    }


    
public Class<? extends Object> getSecureObjectClass() {
        
return FilterInvocation.class;
    }


    
public void invoke(FilterInvocation fi) throws IOException,
            ServletException 
{
        InterceptorStatusToken token 
= super.beforeInvocation(fi);
        
try {
            fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
        }
 finally {
            
super.afterInvocation(token, null);
        }

    }


    
public SecurityMetadataSource obtainSecurityMetadataSource() {
        
return this.securityMetadataSource;
    }


    
public void setSecurityMetadataSource(
            FilterInvocationSecurityMetadataSource newSource) 
{
        
this.securityMetadataSource = newSource;
    }


    @Override
    
public void destroy() {
    }


    @Override
    
public void init(FilterConfig arg0) throws ServletException {
    }


}
最核心的代码就是invoke方法中的InterceptorStatusToken token = super.beforeInvocation(fi);这一句,即在执行doFilter之前,进行权限的检查,而具体的实现已经交给accessDecisionManager了,下文中会讲述。

4,来看看authentication-provider的实现:
package com.robin.erp.fwk.security;
import java.util.ArrayList;
import java.util.Collection;

import org.springframework.dao.DataAccessException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

public class MyUserDetailService implements UserDetailsService {

    @Override
    
public UserDetails loadUserByUsername(String username)
            
throws UsernameNotFoundException, DataAccessException {
        Collection
<GrantedAuthority> auths=new ArrayList<GrantedAuthority>();
        GrantedAuthorityImpl auth2
=new GrantedAuthorityImpl("ROLE_ADMIN");
        auths.add(auth2);
        
if(username.equals("robin1")){
            auths
=new ArrayList<GrantedAuthority>();
            GrantedAuthorityImpl auth1
=new GrantedAuthorityImpl("ROLE_ROBIN");
            auths.add(auth1);
        }

        
//        User(String username, String password, boolean enabled, boolean accountNonExpired,
//                    boolean credentialsNonExpired, boolean accountNonLocked, Collection<GrantedAuthority> authorities) {
        User user = new User(username,
                
"robin"truetruetruetrue, auths);
        
return user;
    }

    
}
在这个类中,你就可以从数据库中读入用户的密码,角色信息,是否锁定,账号是否过期等,我想这么简单的代码就不再多解释了。

5,对于资源的访问权限的定义,我们通过实现FilterInvocationSecurityMetadataSource这个接口来初始化数据。
package com.robin.erp.fwk.security;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.AntUrlPathMatcher;
import org.springframework.security.web.util.UrlMatcher;

/**
 * 
 * 此类在初始化时,应该取到所有资源及其对应角色的定义
 * 
 * 
@author Robin
 * 
 
*/

public class MyInvocationSecurityMetadataSource
        
implements FilterInvocationSecurityMetadataSource {
    
private UrlMatcher urlMatcher = new AntUrlPathMatcher();;
    
private static Map<String, Collection<ConfigAttribute>> resourceMap = null;

    
public MyInvocationSecurityMetadataSource() {
        loadResourceDefine();
    }


    
private void loadResourceDefine() {
        resourceMap 
= new HashMap<String, Collection<ConfigAttribute>>();
        Collection
<ConfigAttribute> atts = new ArrayList<ConfigAttribute>();
        ConfigAttribute ca 
= new SecurityConfig("ROLE_ADMIN");
        atts.add(ca);
        resourceMap.put(
"/index.jsp", atts);
        resourceMap.put(
"/i.jsp", atts);
    }


    
// According to a URL, Find out permission configuration of this URL.
    public Collection<ConfigAttribute> getAttributes(Object object)
            
throws IllegalArgumentException {
        
// guess object is a URL.
        String url = ((FilterInvocation)object).getRequestUrl();
        Iterator
<String> ite = resourceMap.keySet().iterator();
        
while (ite.hasNext()) {
            String resURL 
= ite.next();
            
if (urlMatcher.pathMatchesUrl(resURL, url)) {
                return resourceMap.get(resURL);
            }

        }

        
return null;
    }


    
public boolean supports(Class<?> clazz) {
        
return true;
    }

    
    
public Collection<ConfigAttribute> getAllConfigAttributes() {
        
return null;
    }


}
看看loadResourceDefine方法,我在这里,假定index.jsp和i.jsp这两个资源,需要ROLE_ADMIN角色的用户才能访问。
这个类中,还有一个最核心的地方,就是提供某个资源对应的权限定义,即getAttributes方法返回的结果。注意,我例子中使用的是AntUrlPathMatcher这个path matcher来检查URL是否与资源定义匹配,事实上你还要用正则的方式来匹配,或者自己实现一个matcher。

6,剩下的就是最终的决策了,make a decision,其实也很容易,呵呵。
package com.robin.erp.fwk.security;
import java.util.Collection;
import java.util.Iterator;

import org.springframework.security.access.AccessDecisionManager;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;


public class MyAccessDecisionManager implements AccessDecisionManager {

    
//In this method, need to compare authentication with configAttributes.
    
// 1, A object is a URL, a filter was find permission configuration by this URL, and pass to here.
    
// 2, Check authentication has attribute in permission configuration (configAttributes)
    
// 3, If not match corresponding authentication, throw a AccessDeniedException.
    public void decide(Authentication authentication, Object object,
            Collection
<ConfigAttribute> configAttributes)
            
throws AccessDeniedException, InsufficientAuthenticationException {
        
if(configAttributes == null){
            
return ;
        }

        System.out.println(object.toString());  
//object is a URL.
        Iterator<ConfigAttribute> ite=configAttributes.iterator();
        
while(ite.hasNext()){
            ConfigAttribute ca
=ite.next();
            String needRole
=((SecurityConfig)ca).getAttribute();
            
for(GrantedAuthority ga:authentication.getAuthorities()){
                
if(needRole.equals(ga.getAuthority())){  //ga is user's role.
                    return;
                }

            }

        }

        
throw new AccessDeniedException("no right");
    }


    @Override
    
public boolean supports(ConfigAttribute attribute) {
        
// TODO Auto-generated method stub
        return true;
    }


    @Override
    
public boolean supports(Class<?> clazz) {
        
return true;
    }



}

在这个类中,最重要的是decide方法,如果不存在对该资源的定义,直接放行;否则,如果找到正确的角色,即认为拥有权限,并放行,否则throw new AccessDeniedException("no right");这样,就会进入上面提到的403.jsp页面。

参考资料:
1,Spring官方网站:http://www.springframework.org
2,文章所用的代码,MyEclipse工程,去掉了lib,请自行下载Spring Security 3.x的包,并copy至对应目录。工程源代码
3,根据网络上的资料,制作的CHM版的 Spring Security 3.x 参考手册中文版
4,2009年3月,我在“IBM WebSphere技术专家沙龙(华南区广州站)”演讲时的PPT:《Spring Security--Protect your web application》,当时是Spring Security 2.x,很多原理是一样,可作参考。

教程中为了尽可能不跟其它框架关联上,所以去掉了访问数据库的部分,比如用户信息和资源配置信息的读取,直接写死在代码中了,大家可以根据自己的实际情况补充完整。

如有任何疑问,欢迎大家以评论的方式提问,也欢迎大家讨论!
posted on 2010-03-10 12:38 Robin's Programming World 阅读(59105) 评论(67)  编辑  收藏 所属分类: Java

评论:
# re: Spring Security 3.x 完整入门教程 2010-03-12 10:23 | 看你想吐
是在看不出来,哪里像3.x,3.x的精髓部分根本就没有说明,入门教程有点浅,你这个顶多算是一个小实验而已。
放心吧,你不会在任何地方看见转载的。  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-03-12 13:38 | 楼上你我看你想拉屎
楼上的你看谁的想吐?你P能出个例子出来瞧瞧?标准的人型J8,就张了一张P嘴?标准的嘴子货,在这唧唧歪歪的,不想学习的滚?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-03-14 09:57 | Messenger
学习了!!!  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-03-15 12:30 | 蓝剑
已经能够显示出3.x和2.x的不同了,学习了  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-03-16 10:30 | 看你想吐
@楼上你我看你想拉屎
自古二楼出傻B,这一定律仍在继续,我的ss3你根本就看不懂,我贴出来你还上火,这样的文章你也来顶,你能找到工作,我给你开资,垃-圾。  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-03-19 09:18 | 看你想吐就吐
向你这种随意践踏别人的文章,不尊重别人劳动成果,只会写一篇别人都看不懂的ss3的人...我可以替你妈妈告诉你:你妈妈叫你回家去吐...  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-03-28 18:12 | 第二次看了色
@看你想吐就吐
springsecurity3真是好东西,不过他完全写到点上,还叫什么完整,居然还发布误导人,还不让转载,要说别的我到不说话了,说道springsecurity我从acegi0.x版本就一直用,我都不敢轻易的发一些完整这类话的文章,就看你对ss3的理解,你就是一个入门尚浅的低级用户,还敢说完整。想出名,想教别人,必须自己要会很多,付出很多,否则会贻笑大方的。  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-03-29 14:03 | 大江东流
恩,虽说文章不是很好,看不出那里是实践得来的,倒像是helloworld,不过入门的话,也许能行吧?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-03-30 12:11 | Robin's Java World
看了大家的评论,觉得自己应该更加努力,研究好Spring Security来帮助大家提高。
SS确实是个好东西,我们曾经在多个项目里不只一次的使用过,也是从最初的Acegi开始,并且实现了很多电信级的需求,比如登录3次失败锁定账号,24小时自动解锁,不允许同一帐号同时在多个客户端登录等。  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2010-04-10 08:52 | roypayne
看了前面的评论,真不敢恭维。
都是搞技术的,还这么浮躁,有意思嘛。
你觉得不怎么样,可以不说风凉话吗?
有时间,有这个精力,你怎么不拿出一篇文章来,又贴图,又码字的。

鼓励一下楼主,谢谢分享!  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-04-12 16:12 | 来如风
@看你想吐
你认为核心在那里呢,楼主标题说了,"入门" 你丫就不能看看这两个字吗!!  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-04-14 14:33 | Arnold
非常好,如果能有个demo下载就好了  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-04-20 09:18 | Robin's Java World
@Arnold
文章的参考资料里就有demo呀。  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-05-17 10:23 | sen
你好
部属了你的代码,我把resourceMap.put("/i.jsp", atts);注释掉后发现i.jsp还是可以访问?
这是说明,没有被保护的资源可以访问吧?

我想看到403页面,所以紧接着加上如下代码:
Collection<ConfigAttribute> atts2 = new ArrayList<ConfigAttribute>();
ConfigAttribute ca2 = new SecurityConfig("ROLE_SEN");
atts2.add(ca2);
resourceMap.put("/i.jsp", atts2);
再用robin登录访问i.jsp
小弟不才,经多次测试还是没看到想看到的403页面?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-05-28 12:40 | Ganky
不错的东西~~我初学Spring Security,你这篇文章给我很大的帮助!
不过有个问题,你的pathMatchesUrl方法,两个参数搞反了……
让我纠结了一个早上

最后还是感谢你的贡献  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-06-25 18:09 | chen3975
public class MyInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
private RoleService roleService;
public RoleService getRoleService() {
return roleService;
}
public void setRoleService(RoleService roleService) {
this.roleService = roleService;
}
private UrlMatcher urlMatcher = new AntUrlPathMatcher();
private static Map<String, Collection<ConfigAttribute>> resourceMap = null;

public Collection<ConfigAttribute> getAllConfigAttributes() {
return null;
}
/**
*采用有参数的构造函数
*RoleService 是获取资源的类
**/
public MyInvocationSecurityMetadataSource(RoleService roleService) {
this.roleService = roleService;
loadResourceDefine();
}

private void loadResourceDefine() {
resourceMap = new HashMap<String, Collection<ConfigAttribute>>();
List<Role> roles = roleService.findAllRoles();
if(roles!=null && roles.size()>0){
for(Role o:roles){
ConfigAttribute ca = new SecurityConfig(o.getName());
List<Resource> resources = o.getResources();
if(resources!=null && resources.size()>0){
for(Resource resource:resources){
if(resourceMap.containsKey(resource.getResourceString())){
resourceMap.get(resource.getResourceString()).add(ca);
}else{
Collection<ConfigAttribute> atts = new ArrayList<ConfigAttribute>();
atts.add(ca);
resourceMap.put(resource.getResourceString(), atts);
}
}
}
}
}else{
ConfigAttribute ca = new SecurityConfig("ROLE_ADMIN");
Collection<ConfigAttribute> atts = new ArrayList<ConfigAttribute>();
atts.add(ca);
resourceMap.put("/index.jsp", atts);
resourceMap.put("/i.jsp", atts);
}
/**
ConfigAttribute ca = new SecurityConfig("ROLE_ADMIN");
atts.add(ca);
resourceMap.put("/index.jsp", atts);
resourceMap.put("/i.jsp", atts);
*/
}

public Collection<ConfigAttribute> getAttributes(Object arg0)
throws IllegalArgumentException {
String url = ((FilterInvocation)arg0).getRequestUrl();
Iterator<String> ite = resourceMap.keySet().iterator();
while (ite.hasNext()) {
String resURL = ite.next();
if (urlMatcher.pathMatchesUrl(url, resURL)) {
return resourceMap.get(resURL);
}
}
return null;
}

public boolean supports(Class<?> arg0) {
return true;
}

}


<!--
MyInvocationSecurityMetadataSource 配置修改如下
资源源数据定义,即定义某一资源可以被哪些角色访问
-->
<beans:bean id="securityMetadataSource"
class="com.shoesishow.security.MyInvocationSecurityMetadataSource">
<beans:constructor-arg><beans:ref bean="roleService"/></beans:constructor-arg>
</beans:bean>  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-06-25 18:10 | chen3975
上面的是通过动态读取数据库来实现的.请结合自身情况改改.其实楼主已经把例子写的挺好的了.  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-06-26 17:55 | luckyzhang
FilterInvocationSecurityMetadataSource的实现中,如果需要注入获取数据库资源的dao,会提示:java.lang.NullPointerException,这点楼主并没有解决。而这点通常是很重要的,如果仅仅是hardcode,直接在配置文件中写都ok了,希望楼主可以解答。不要告诉我用jdbc直接取,这种代码不优雅!  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-06-26 17:56 | luckyzhang
最近做项目,在这里卡住了,希望楼主见贴答复,谢谢!qq:4639966, mail:cngdzql#gmail.com  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-06-28 09:51 | chen3975
@luckyzhang
我不是给你解决办法了.
就是采用有参数的构造方法来解决注入你要的属性例如:
public MyInvocationSecurityMetadataSource(RoleService roleService) {
this.roleService = roleService;
loadResourceDefine();
}
RoleService 是可以获得资源列表的组件.
在xml配置文件中采用构造函数注入的方式把RoleService 注入到MyInvocationSecurityMetadataSource中.例如
<beans:bean id="securityMetadataSource"
class="com.shoesishow.security.MyInvocationSecurityMetadataSource">
<beans:constructor-arg><beans:ref bean="roleService"/></beans:constructor-arg>
</beans:bean>
我上面写的很清楚.  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-07-04 23:07 | roamer
@chen3975
你这个使用dao的方法也挺好。但是如果能在MyInvocationSecurityMetadataSource类里面直接使用@Inject方法,就更符合非配置的annonation方法来。不知道有没有解决办法  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-08-18 21:15 | 感谢楼主
真感谢楼主提供这么好的技术文献,解决了小弟的一大难题!
期待楼主贡献更多的Spring-Security3的技术文献!
谢谢!  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2010-08-20 18:48 | Leo
我也看不到403页面,而且名字为空的话弹不出js脚本,很奇怪的问题

随意改变用户名也可以正常登录……怎么会这样呢?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2010-08-20 20:29 | Leo
原来403页面就是i.jsp啊,明白了。

但是仍然无法弹出js脚本提示,而且提交表单form的action值为什么一定要为j_spring_security_check呢?

index.jsp中的logout按钮的href也是固定为j_spring_security_logout的

不能自定义么?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-08-28 19:39 | 感谢楼主
MyInvocationSecurityMetadataSource类里的 resourceMap 为什么要定义为static ?
请不我不设置为 static 可以吗 ?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2010-09-06 16:13 | 呵呵
有代码吗?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-09-06 19:53 | yuxuan
正在学习spring security,非常感谢您的教程!  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-09-12 17:50 | Robin's Java World
@呵呵
请见参考资料2  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-09-19 19:09 | hermes
貌似有点问题,访问的url不在定义的访问内,还能访问?不知道问题出在哪里了?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-09-19 20:30 | hermes
MyAccessDecisionManager
我咋发现当url匹配的时候才调用这个。。。而且没有定义的照样可以访问~~
楼主没有遇到这个问题吗?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-09-19 21:11 | hermes
如果不存在对该资源的定义,直接放行;
晕,没有看到这句话。。。在问一句:
spring security支持角色继续吗?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-09-26 08:34 | Robin's Java World
@hermes
当然支持角色。  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2010-09-28 15:59 | 难得糊涂
lz水平确实一般,技术水平与文章功底比较弱,只是从自己角度解释问题;
自己学习总结倒也挺好,但是什么开篇一些不得转载装大拿的话太惹人嫌,比较肤浅,在别人看来如跳梁小丑一般啊。。。  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2010-09-28 17:53 | 天道酬勤
请问下 ,在MyInvocationSecurityMetadataSource中,能不能通过spring的标注注入对象,然后使用比如。
@Autowired
private FunctionManager functionManager;

但是我的怎么是空指针?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-12-27 01:29 | 越王够贱
@天道酬勤 我的也出了空指针啊!不知道怎么回事你搞懂了么
  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2010-12-27 01:31 | 越王够贱
@天道酬勤我的出在这里谁给我看下啊
resourceMap = new HashMap<String, Collection<ConfigAttribute>>();
System.out.println("afdsfds");

for(Resources item:securityresourcedao.getAllResource()){
resourceMap.put(item.getUrl(),listToCollection(item.getRole()));
}
连securityresourcedao.getAllResource()这个方法进都没进!
@Repository
@SuppressWarnings("unchecked")
public class SecurityResourceDaoImp implements SecurityResourceDao{
@Autowired
private SessionFactory getsessionfactory;
public Session getSession(){
Session session=getsessionfactory.getCurrentSession();
return session;
}
public List<Resources> getAllResource() {
Session se=this.getSession();
String hql="from Resource";
List<Resources> listresource=se.createQuery(hql).list();
return listresource;
}
  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2011-04-18 15:51 | ajaxljk
org.springframework.security.web.util.UrlMatcher
这个接口在spring-security3.1里面没有了,是怎么回事,有它的替代接口吗?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2011-05-05 09:24 | junxy
这个问题我也遇到了 不存在资源定义时根本就不会走到decide方法里面去,不知道大家遇到没?@hermes
  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2011-07-25 09:14 | 草i你妈
还惹上管事

垃圾一个  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2011-07-25 09:18 |
2009年3月,我在“IBM WebSphere技术专家沙龙(华南区广州站)”演讲时的PPT


我晕 鸟人 知道你是那种人了
讲ppt的人了 就知道注重面子工程 写过代码没?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2011-08-11 15:35 | one new worker
楼主,很多没接粗过spring security的人,更需要你的帮助,配置spring security真的好麻烦,能不能给一份重头开始的配置呢?能加一些配置过程中的图片就更好了。建完web project后一步一步配置的操作,网上没有见到过这样类似的配置,要知道你少些几句话会让初学者走很多弯路的。
感谢楼主!!!

xiefh2011@foxmail.com  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2011-11-30 12:32 |
你好,最近看了你的这篇博客,可是我还是不明白如果加入SSH框架,并且要验证密码是都正确的话应该怎么弄,还希望您不吝赐教,小弟万分感谢。邮箱:4926664447@qq.com  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2011-11-30 15:32 | 顶楼主
我是新人,以前从未弄过Spring Security,甚至今天之前还不知道Spring Security是个什么东东。 看了一遍楼主的这篇帖子后,表示还是不懂。希望楼主给点更详细的,感谢  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2012-04-20 15:13 | S
@看你想吐就吐
SHIT  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2012-04-20 15:15 | S
@楼上你我看你想拉屎
SON FO BITCH  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2012-08-23 17:12 | Mr. Yang
谢谢楼主分享,学习了  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2012-08-29 10:17 | fuck
网上的垃圾已经够多了  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2012-10-09 16:47 | 无名
楼主我问一下你都导入了哪些jar包  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2012-10-15 15:07 | barbarainboy
不错的帖子,对于我这样的新手来说,真是如鱼得水了~~~~~~~~~~~~~~~~~~~  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2013-01-24 15:39 | IT小鸟
你好,我想请教一下403页面的配置
特别是在403中获取throw new AccessDeniedException("no right");
这个异常的 信息  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2013-04-18 23:45 |
好好读一读  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2013-05-02 18:18 | JAKE
例子很好,谢谢,但是其中有写类,被spring 废弃了,所以应该有更好的示例。楼主无视那些喷子吧。  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2013-05-07 09:45 | 日明月易
本人初学者,请问要导入哪些jar包啊,我导入了Spring Security的所有包加上Spring的所有包还是报错
013-5-7 9:38:52 org.apache.catalina.core.StandardContext listenerStart
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from file [D:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\SpringSecurityDemo\WEB-INF\classes\applicationContext-security.xml]; nested exception is java.lang.NoSuchMethodError: org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.getLocalName(Lorg/w3c/dom/Node;)Ljava/lang/String;
好像是无法解析XML文档吗,这个项目用到的jar包楼主能否贴出来,谢谢啦
  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2013-06-12 23:32 | 蜗牛
你这个是个包冲突,我前天也是这样,后来换了包就好了,包冲突真的很蛋碎。。。@日明月易
  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2013-08-16 10:15 | zzj
@看你想吐就吐
妈的,写的代码让人容易理解那才叫高手,你Y的写的SS3让人看不懂,那叫啥啊?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2013-09-17 14:46 | 过客
看不懂别人写的东西,就谦虚点,别人不欠你的! 要么离开!!
尊重:这是做人最起码的!  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2013-10-20 22:04 | 路过
这个也叫完整教程  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2013-11-13 16:33 | 初学
离帖子都几年了。。。接触Spring Security有3天了,一直没有例子看看,这个不错了,帮助很大
  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2013-11-29 17:49 | Neddy
<input type="text" name="xxx">  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2014-01-22 15:57 | name
回帖里面有些看起来很牛逼的人,发个教程吧。  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2014-02-24 10:03 | 零度
请问楼主按您的方法我的应用报了:java.lang.IllegalArgumentException: AccessDecisionManager does not support secure object class: class org.springframework.security.web.FilterInvocation 这样的错。请问是什么问题怎么样解决  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2014-10-10 10:55 | sailer
请问楼主我的应用报了:Caused by: java.lang.IllegalArgumentException: SecurityMetadataSource does not support secure object class: class org.springframework.security.web.FilterInvocation 网上搜了很久也没有相关的答案,不知楼主能否给解答下。非常感谢!  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程[未登录] 2014-10-10 12:28 | sailer
我也遇到了这个错误,后面发现可能和决策器里面的supports函数有关,默认返回false,改成返回true之后就能启动了,哦 还有给资源授权的那个类里面的supports函数也返回true就可以了 @零度
转自http://edwin492.iteye.com/blog/1151789  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2014-12-26 17:26 | smile雪泪寒
楼主有源码吗?麻烦发一份完整的Demo+sql文件给我,谢谢,1490020533 qq  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2015-05-28 10:38 | Jackie
有个问题:角色和模块的关系建立是在启动的时候。一般的运行程序后台添加关系,这样新添加的关系就没用了,这个如何解决?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2015-09-09 14:27 | iechenyb
AntUrlPathMatcher这个类所在jar包是哪个啊,是spring-security-core-tiger这个吗?  回复  更多评论
  
# re: Spring Security 3.x 完整入门教程 2016-01-04 17:40 | bns
@过客


尊重人是最起码的  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: