ITeye博客

MyEclipse中Spring Security 3.0.3无废话配置(第二章)。

andy_ghg · 2011-06-12 17:30 · Java · 企业架构 · Myeclipse · DAO · Spring · Access · Security · Markdown

上回说到数据库验证首先看以往的验证配置: [code="xml"] [/code] 现在我们要用数据库来验证。 第一步:建立数据库 这里只用两张表即可解决问题,也许你看过很多建立五张到七张的,那是实际在项目中才这样做,我们做试验的话在这里只建立两张表:1.用户表2.资源表。 用户表四个字段:ID、USERNAME、PASSWORD、ROLE(权限) 资源表有三个字段:ID、资源路径、访问权限 资源路径就是一个URL地址,访问权限就是这个资源需要什么样子的权限才能访问。 第二步:权限提供 [code="java"] package cn.com.fri.security.supports; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; 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; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import cn.com.fri.security.dao.interfaces.IAuthoritiesDAO; import cn.com.fri.security.dao.interfaces.IResourcesDAO; import cn.com.fri.security.vo.Authorities; import cn.com.fri.security.vo.Resources; /** * * 此类在初始化时,应该取到所有资源及其对应角色的定义 * */ @Service("securityMetadataSource") public class MyInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource { @Autowired private IAuthoritiesDAO authoritiesDAO; @Autowired private IResourcesDAO resourcesDAO; private UrlMatcher urlMatcher = new AntUrlPathMatcher();; private static Map> resourceMap = null; public MyInvocationSecurityMetadataSource() { } /** * 在此处,将数据库中所有的资源以及对应的权限加入到内存中 */ @PostConstruct public void loadResourceDefine() { resourceMap = new HashMap>(); Collection atts = new ArrayList(); System.out.println(authoritiesDAO); List auth = authoritiesDAO.findAll(); for (Authorities au : auth) { ConfigAttribute ca = new SecurityConfig(au.getAuthorname()); atts.add(ca); } List res = resourcesDAO.findAll(); for (Resources resources : res) { resourceMap.put(resources.getResourcesstring(), atts); } System.out.println("权限加载完毕"); } // According to a URL, Find out permission configuration of this URL. @Transactional(readOnly = true) public Collection getAttributes(Object object) throws IllegalArgumentException { // guess object is a URL. String url = ((FilterInvocation) object).getRequestUrl(); Iterator 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 clazz) { return true; } public Collection getAllConfigAttributes() { return null; } public void setAuthoritiesDAO(IAuthoritiesDAO authoritiesDAO) { this.authoritiesDAO = authoritiesDAO; } public void setResourcesDAO(IResourcesDAO resourcesDAO) { this.resourcesDAO = resourcesDAO; } } [/code] 找个类的作用就是在服务器启动的时候将存放在数据库表中的资源与访问权限加载到内存中去。 第三步:编写路径验证类: [code="java"] package cn.com.fri.security.supports; 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; import org.springframework.stereotype.Service; @Service("myAccessDecisionManagerBean") public class MyAccessDecisionManager implements AccessDecisionManager { public void decide(Authentication authentication, Object object, Collection configAttributes) throws AccessDeniedException, InsufficientAuthenticationException { if (configAttributes == null) { return; } Iterator 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"); } public boolean supports(ConfigAttribute attribute) { // TODO Auto-generated method stub return true; } public boolean supports(Class clazz) { return true; } } [/code] 此类用户判断当前登录的用户是否有权限访问某个路径。而这里路径的访问权限就是从内存中取出来的。 第四步:登录验证: [code="java"] package cn.com.fri.security.supports; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.GrantedAuthorityImpl; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import cn.com.fri.security.dao.interfaces.IUsersDAO; import cn.com.fri.security.vo.Authorities; import cn.com.fri.security.vo.CustomUser; import cn.com.fri.security.vo.Users; @Service("userDetailsService") public class UserDetailsServiceImpl implements UserDetailsService { @Autowired private IUsersDAO dao; @Transactional(readOnly = true) public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException { System.out.println(username); Users user = dao.findByUsername(username).get(0);//此查询由自己去实现 if (user == null) throw new UsernameNotFoundException("user not found"); Collection authorities = new ArrayList(); List auths = dao.findAutByUsername(username);//此查询由自己去实现如果用户表中已经包含了权限,就无需进行这一步操作了。 System.out.println(">>>>>>获得的权限有:" + auths.size() + "个"); for (Authorities a : auths) { GrantedAuthorityImpl authorityImpl = new GrantedAuthorityImpl(a .getAuthorname()); authorities.add(authorityImpl); } CustomUser u = new CustomUser(username, user.getPsw(), user .getEnabled(), true, true, true, authorities); u.setUser(user); return u; } } [/code] 在这个类中,用户一旦登录,我们就能获取到用户的权限,将用户权限放入到CustomUser中,然后返回这个CustomUser即可。CustomUser为继承自org.springframework.security.core.userdetails.User的一个类,继承的原因是为了让原来的User能够拥有更多的属性,例如用户的中文名称等等。里面的List auths = dao.findAutByUsername(username);这一步是因为我是按照七张表的结构去建立的,因此如果你只有两张表,这里完全可以省略。你甚至可以在这里写死(试验用)如下: [code="java"] GrantedAuthorityImpl authorityImpl = new GrantedAuthorityImpl("ROLE_ADMIN"); authorities.add(authorityImpl); [/code] 第五步:配置SS3的XML文件: [code="xml"] [/code] 由于东西较多,所以再一开始的时候建议大家去下载官方的说明文档和中文翻译的说明文档。 在此我们的配置结束。这一部分虽然代码比较多,但是其实一点不复杂,仔仔细细的看一遍就会了。

评论(6)

  1. #6 楼 · 516816168 · 2011-08-19 11:22

    详细,不错啊

  2. #5 楼 · mfx258 · 2011-06-13 16:28

    andy_ghg writes:

    就是登录后根本没有进入到UserDetailsServiceImpl这个类是吗?

    进入这个类了,下面是后台日志输出: INFO : com.sywzsh.security.PawnUsersDetailsService - auths=== INFO : com.sywzsh.security.dao.SysUsersDao - 根据UserAccount查找SysUsers实例对象: admin

    INFO : com.sywzsh.security.dao.SysUsersDao - 相匹配的SysUsers实例对象被找到!

    问题是在这一步

    
    public Collection<ConfigAttribute> getAttributes(Object object)  
                throws IllegalArgumentException {  
            // guess object is a URL.  
            String url = ((FilterInvocation) object).getRequestUrl();  
            log.info("url====="+url);
            Iterator<String> ite = resourceMap.keySet().iterator();  
            while (ite.hasNext()) {  
                String resURL = ite.next();  
                if (urlMatcher.pathMatchesUrl(url, resURL)) {  
                    return resourceMap.get(resURL);  
                }  
            }  
            return null;  
        }  
    
    
    

    其中:log.info("url====="+url);打印出的是/jsp/fail.jsp,这个链接是在security.xml中配置的认证失败跳转页面

    
    <form-login login-page="/jsp/admin/login.jsp" login-processing-url="/topic/add"
    			authentication-failure-url="/jsp/fail.jsp" default-target-url="/jsp/index.jsp" />
    
    

    点登录按钮,应该判断/jsp/index.jsp是否有权限,不知道为什么直接就跳到失败页面了

  3. #4 楼 · andy_ghg · 2011-06-13 15:23

    就是登录后根本没有进入到UserDetailsServiceImpl这个类是吗? 首先看一下是否进入了这个类,并且成功调用了loadUserByUsername这个方法(此方法是UserDetailsService接口提供的)。

    按理来说,点击登录之后是应该调用这个方法的。如果没有调用,你查看一下这里的配置是否正确:

    
    	<!-- 配置登录验证的类 -->
    	<beans:bean id="daoAuthenticationProvider"
    		class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <!--------注意这里------------>
    		<beans:property name="userDetailsService" ref="userDetailsService" />
    	</beans:bean>
    
    	<beans:bean id="authenticationManager"
    		class="org.springframework.security.authentication.ProviderManager">
    		<beans:property name="providers">
    			<beans:list>
    				<beans:ref local="daoAuthenticationProvider" />
    			</beans:list>
    		</beans:property>
    	</beans:bean>
    	
    	<authentication-manager>
    <!--------注意这里------------>
    		<authentication-provider user-service-ref="userDetailsService">
    		</authentication-provider>
    	</authentication-manager>
    
    

    里面的ref是参考了UserDetailServiceImple的annocation:

    
    @Service("userDetailsService")  
    public class UserDetailsServiceImpl implements UserDetailsService {  
        //..............
    }
    
    
  4. #3 楼 · mfx258 · 2011-06-13 10:26

    我扩展了userDetails 代码如下

    
    List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
    		
    		//得到用户的权限
    		auths = sysUsersDao.loadUserAuthoritiesByUserName( username );
    		
    		log.info("auths==="+auths);
    		
    		//根据用户名取得一个SysUsers对象,以获取该用户的其他信息。
    		SysUsersBean user = sysUsersDao.findByUserName( username );
    		log.info("user===="+user);
    		return new SysUsers( user.getUserId(), user.getUserAccount(), user.getUserName(),
    				 user.getUserPassword(),user.getUserDesc(), user.getEnabled(), user.getIssys(),
    				 user.getUserDuty(), user.getUserDept(), 
    				 new HashSet(0), true, true, true, auths);
    
    

    如果未登录,直接跳到登陆页面,比如访问/jsp/admin/security/role.jsp,后台输出

    
    

    INFO : com.sywzsh.security.PawnInvocationSecurityMetadataSourceService - getAttributes(Object)=========/jsp/admin/security/role.jsp INFO : com.sywzsh.security.PawnAccessDecisionManagerService - decide(Authentication, Object, Collection) - start INFO : com.sywzsh.security.PawnAccessDecisionManagerService - 正在访问的url是:FilterInvocation: URL: /jsp/admin/security/role.jsp INFO : com.sywzsh.security.PawnAccessDecisionManagerService - needRole is:AUTH_XTSZ_USER INFO : com.sywzsh.security.PawnAccessDecisionManagerService - 授权信息是:ROLE_ANONYMOUS

    这个是正确的,但输入用户名和密码后,再点登录,默认应该跳到

    
    default-target-url="/index.jsp"
    
    

    但后台什么输出都没有,直接又回到登录页面了

  5. #2 楼 · andy_ghg · 2011-06-13 08:53

    mfx258 writes:

    您好,按照您的配置,启动没有问题,只是在登录时,点击登录后直接跳到验证失败页面,debug跟踪发现,InvocationSecurityMetadataSourceService中的getAttributes(Object object)方法传入的object值就是验证失败页面的url,还有什么地方需要配置吗?

    你好,首先看一下UserDetailsServiceImpl这个类中的

    
       CustomUser u = new CustomUser(username, user.getPsw(), user  
                    .getEnabled(), true, true, true, authorities);  
    
    

    有没有执行到这里。并且里面的authorities是否包含了权限,既:

    
    GrantedAuthorityImpl authorityImpl = new GrantedAuthorityImpl("ROLE_ADMIN");  
    authorities.add(authorityImpl);  
    
    

    有没有类似的代码?

    如果有的话,你的数据库中的资源所需权限是否与之相等,例如: URL ROLE /index.jsp ROLE_ADMIN

  6. #1 楼 · mfx258 · 2011-06-12 22:42

    您好,按照您的配置,启动没有问题,只是在登录时,点击登录后直接跳到验证失败页面,debug跟踪发现,InvocationSecurityMetadataSourceService中的getAttributes(Object object)方法传入的object值就是验证失败页面的url,还有什么地方需要配置吗?

评论功能维护中,当前仅展示历史评论。