---
title: "MyEclipse中Spring Security 3.0.3无废话配置（第二章）。"
author: "andy_ghg"
date: "2011-06-12T17:30:29Z"
tags: ["Access", "DAO", "Myeclipse", "Security", "Spring"]
categories: ["企业架构"]
slug: "myeclipse-spring-security-3-0-3"
draft: false
lastmod: "2011-06-12T17:30:29Z"
id: 1081622
url: "https://andy-ghg.iteye.com/blog/1081622"
---

上回说到数据库验证首先看以往的验证配置： \[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&gt; resourceMap = null; public MyInvocationSecurityMetadataSource() { } /\** * 在此处，将数据库中所有的资源以及对应的权限加入到内存中 \*/ @PostConstruct public void loadResourceDefine() { resourceMap = new HashMap&gt;(); 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("&gt;&gt;&gt;&gt;&gt;&gt;获得的权限有：" + 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] 由于东西较多，所以再一开始的时候建议大家去下载官方的说明文档和中文翻译的说明文档。 在此我们的配置结束。这一部分虽然代码比较多，但是其实一点不复杂，仔仔细细的看一遍就会了。