SpringSecurity3安全權限管理手冊_第1頁
SpringSecurity3安全權限管理手冊_第2頁
SpringSecurity3安全權限管理手冊_第3頁
SpringSecurity3安全權限管理手冊_第4頁
SpringSecurity3安全權限管理手冊_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Spring Security 3.0 安全權限管理手冊參考文獻:1、中的spring security權限管理手冊。2、spring security3.0權限管理手冊3、spring的相關資料。本文檔內容僅作為公司權限管理資料用,對于企業(yè)來說,權限管理將是系統(tǒng)中的非常重要的一個模塊,權限的設計也是參考相關資料進行整理和補充。系統(tǒng)將通過數據庫進行管理用戶權限。權限管理搭建要的問題:1、區(qū)分Authentication(驗證)和 Authorization(授權)驗證這個用戶是誰?用戶身份可靠嗎?授權某用戶A是否可以訪問資源R某用戶A是否可以執(zhí)行M操作某用戶A是否可以對資源R執(zhí)行M操作2、SS

2、中的驗證特點支持多種驗證方式支持多種加密格式支持組件的擴展和替換可以本地化輸出信息3、SS中的授權特點支持多種仲裁方式支持組件的擴展和替換支持對頁面訪問、方法訪問、對象訪問的授權。4、SS核心安全實現(xiàn)Web安全通過配置Servlet Filter激活SS中的過濾器鏈實現(xiàn)Session一致性驗證實現(xiàn)免登陸驗證(Remember-Me驗證)提供一系列標簽庫進行頁面元素的安全控制方法安全通過AOP模式實現(xiàn)安全代理Web安全與方法安全均可以使用表達式語言定義訪問規(guī)則5、配置SS配置Web.xml,應用安全過濾器配置Spring,驗證與授權部分在web頁面中獲取用戶身份在web頁面中應用安全標簽庫實現(xiàn)方

3、法級安全6、配置web.xml7、Spring配置文件中設置命名空間8、通過數據庫驗證用戶身份9、完善web頁面驗證規(guī)則10、自定義驗證配置11、本地化消息輸出(國際化) 根據公司項目的開發(fā)要求和集合spring security3.0功能,公司將通過數據庫進行對用戶身份驗證和授權,系統(tǒng)將建立5個基礎表進行對權利的管理。第一部分 數據庫設計1、表設計表1:用戶表(pub_users)序號字段類型含義備注1User_IdVchar(32)用戶idPK2user_accountVchar(30)登陸用戶名(登陸號)3User_nameVchar(40)用戶姓名4user_PasswordVchar

4、(100)用戶密碼5EnabledInt是否被禁用0禁用1正常6isSysInt是否是超級用戶0非1是7user_DEScVchar(100)描述說明:pub_users表中的登錄名和密碼用來控制用戶的登錄。表2:權限表(pub_authorities)序號字段類型含義備注1authority_IdVchar(32)權限idPK2Authority_nameVchar(40)權限名稱3Authority_DEScVchar(100)權限描述4EnabledInt是否被禁用0禁用1正常5isSysInt是否是超級權限0非1是說明:pub_authorities表中描述的是系統(tǒng)擁有哪些權限,如果要

5、詳細分類,可以將一個url定義一個權限,那樣就能對所有資源進行管理。表3:角色表(pub_roles)序號字段類型含義備注1role_IdVchar(32)角色idPK2role_nameVchar(100)角色名稱3role_DEScVchar(100)角色描述4EnabledInt是否被禁用0禁用1正常5isSysInt是否是超級權限0非1是說明:pub_roles表中描述的是系統(tǒng)按用戶分類或按照功能模塊分類,將系統(tǒng)進行整合歸類管理。表4:資源表(pub_resources)序號字段類型含義備注1resource_IdVchar(32)資源idPK2resource_nameVchar(1

6、00)資源名稱3resource _typeVchar(40)資源類型url、method4priorityint資源優(yōu)先權即排序5resource _stringVchar(200)資源鏈接6resource_DEScVchar(100)資源描述7EnabledInt是否被禁用0禁用1正常8isSysInt是否是超級權限0非1是說明:pub_roles表中描述的是系統(tǒng)需要保護的資源及(url或方法)。以上四個表是權限管理的基礎表(用戶表、權限表、角色表、資源表)。表5:用戶角色連接表(pub_users_roles)序號字段類型含義備注1IdIndetityId主鍵PK2user_IdVch

7、ar(32)用戶id3role_idVchar(32)角色id說明:用來管理用戶和角色的關系。表6:角色權限連接表(pub_roles_authorities)序號字段類型含義備注1IdIndetityId主鍵PK2role _IdVchar(32)角色id3authority_IdVchar(32)權限id說明:用來管理角色和權限的關系。表7:權限資源連接表(pub_authorities_resources)序號字段類型含義備注1IdIndetityId主鍵PK2authority_IdVchar(32)權限id3resource_IdVchar(32)資源id說明:用來管理角色和權限的關

8、系。2、建表語句如下(數據庫采用MS SQL 2000):create table pub_users( user_id varchar(32), user_account varchar(30), user_name varchar(40), user_password varchar(100), user_desc varchar(100), enabled int, issys int);alter table pub_users add constraint pk_pub_users primary key(user_id);create table pub_authorities(

9、authority_id varchar(32), authority_name varchar(40), authority_desc varchar(100), enabled int, issys int);alter table pub_authorities add constraint pk_pub_authorities primary key(authority_id);create table pub_roles( role_id varchar(32), role_name varchar(40), role_desc varchar(100), enabled int,

10、issys int);alter table pub_roles add constraint pk_pub_roles primary key(role_id);create table pub_resources( resource_id varchar(32), resource_name varchar(100), resource_desc varchar(100), resource_type varchar(40), resource_string varchar(200), priority int, enabled int, issys int);alter table pu

11、b_resources add constraint pk_pub_resources primary key(resource_id);create table pub_users_roles( id numeric(12,0) IDENTITY NOT NULL, user_id varchar(32), role_id varchar(32), enabled int);alter table pub_users_roles add constraint pk_pub_users_roles primary key(id);alter table pub_users_roles add

12、constraint fk_users_roles_users foreign key(user_id) references pub_users(user_id);alter table pub_users_roles add constraint fk_users_roles_roles foreign key(role_id) references pub_roles(role_id);create table pub_roles_authorities( id numeric(12,0) IDENTITY NOT NULL, role_id varchar(32), authority

13、_id varchar(32), enabled int);alter table pub_roles_authorities add constraint pk_pub_roles_authorities primary key(id);alter table pub_roles_authorities add constraint fk_pub_roles_authorities_authorities foreign key(authority_id) references pub_authorities(authority_id);alter table pub_roles_autho

14、rities add constraint fk_pub_roles_authorities_roles foreign key(role_id) references pub_roles(role_id);create table pub_authorities_resources( id numeric(12,0) IDENTITY NOT NULL, authority_id varchar(32), resource_id varchar(32), enabled int);alter table pub_authorities_resources add constraint pk_

15、pub_authorities_resources primary key(id);alter table pub_authorities_resources add constraint fk_pub_authorities_resources_authorities foreign key(authority_id) references pub_authorities(authority_id);alter table pub_authorities_resources add constraint fk_pub_authorities_resources_resources forei

16、gn key(resource_id) references pub_resources(resource_id);3、E-R圖如下:第二部分 WEB數據庫整合提示:相關代碼請參考項目模塊1、將數據庫表結構和Hibernate建立映射,本系統(tǒng)采用annotation進行對數據庫進行零配置處理(請參考hibernate映射),如圖。2、建立權限的Dao層。3、建立權限的Service層4、配置web.xml rstframewebAppRootKeyrstframe.rootlog4jConfigLocationclasspath:pertieslog4jRefreshInterval60000

17、 contextConfigLocationclasspath*:/applicationContext.xml,classpath*:/applicationContext-rstframe.xmlencodingFilterencodingUTF-8encodingFilter/*hibernateOpenSessionInViewFilterexcludeSuffixsjs,css,jpg,gifhibernateOpenSessionInViewFilter/* springSecurityFilterChain springSecurityFilterChain /* struts2

18、Filterstruts2Filter/* org org.springframework.security.web.session.HttpSessionEventPublisher 20index.jsp/common/500.jsp500/common/500.jsp404/common/404.jsp403/common/403.jsp/WEB-INF/struts-menu-el.tld/WEB-INF/tlds/struts-menu-el.tld/WEB-INF/struts-menu.tld/WEB-INF/tlds/struts-menu.tld/WEB-INF/c.tld/

19、WEB-INF/tlds/c.tld/WEB-INF/fmt.tld/WEB-INF/tlds/fmt.tld/WEB-INF/fn.tld/WEB-INF/tlds/fn.tld/WEB-INF/web-date.tld/WEB-INF/tlds/web-date.tld/WEB-INF/web-flex.tld/WEB-INF/tlds/web-flex.tld/WEB-INF/web-graph.tld/WEB-INF/tlds/web-graph.tld/WEB-INF/web-grid.tld/WEB-INF/tlds/web-grid.tld/WEB-INF/web-html.tl

20、d/WEB-INF/tlds/web-html.tld/WEB-INF/web-list.tld/WEB-INF/tlds/web-list.tld/WEB-INF/web-loushang.tld/WEB-INF/tlds/web-loushang.tld/WEB-INF/web-menu.tld/WEB-INF/tlds/web-menu.tld/WEB-INF/web-multitab.tld/WEB-INF/tlds/web-multitab.tld/WEB-INF/web-seltree.tld/WEB-INF/tlds/web-seltree.tld/WEB-INF/web-tab

21、.tld/WEB-INF/tlds/web-tab.tld/WEB-INF/web-tree.tld/WEB-INF/tlds/web-tree.tld/WEB-INF/web-widgets.tld/WEB-INF/tlds/web-widgets.tld/WEB-INF/web-i18n.tld/WEB-INF/tlds/web-i18n.tld/WEB-INF/gystudio.tld/WEB-INF/tlds/gystudio.tldrarapplication/rar5、配置spring security3.0中的xml文件 文件名:applicationContext-securi

22、ty.xmlSpringSecurity安全配置 !- - !- - 第三部分 SS3.0的實現(xiàn)這是項目的主體部分:這四個類說明如下。用來獲得用戶驗證信息(MyUserDetailService)代碼如下:package com.rstco.frame.pub.security.support;import java.util.ArrayList;import java.util.Collection;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.sp

23、ringframework.dao.DataAccessException;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.userdetails.User;import org.springframework.security.core.userdetails.UserDetails;import org.springframework.security.core.userdetails.UserDetailsService;import or

24、g.springframework.security.core.userdetails.UsernameNotFoundException;import org.springframework.stereotype.Service;import com.rstco.frame.pub.security.dao.PubAuthoritiesResourcesDao;import com.rstco.frame.pub.security.dao.PubUsersDao;import com.rstco.frame.pub.security.entity.PubAuthorities;import

25、com.rstco.frame.pub.security.entity.PubAuthoritiesResources;/你就可以從數據庫中讀入用戶的密碼,角色信息,是否鎖定,賬號是否過期Servicepublic class MyUserDetailService implements UserDetailsService Autowiredprivate PubUsersDao pubUsersDao;Autowiredprivate PubAuthoritiesResourcesDao pubAuthoritiesResourcesDao; public UserDetails load

26、UserByUsername(String username)throws UsernameNotFoundException, DataAccessException Collection auths=new ArrayList();/取得用戶的權限List auth=pubUsersDao.findAuthByUserName(username);String password=null;/取得用戶的密碼password=pubUsersDao.findUserByname(username).get(0).getUserPassword();List aaa=pubAuthorities

27、ResourcesDao.getAll(); User user = new User(username, password, true, true, true, true, auths); return user;最核心的地方,就是提供某個資源對應的權限定義,取得所有角色(auth)的對應資源數據(MyInvocationSecurityMetadataSourceService)代碼如下: package com.rstco.frame.pub.security.support;import java.util.ArrayList;import java.util.Collection;i

28、mport java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import javax.servlet.ServletContext;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Autowired;import org.sprin

29、gframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.security.access.ConfigAttribute;import org.springframework.security.access.SecurityConfig;import org.springframework.security.web.FilterInvocation;import ercept.

30、FilterInvocationSecurityMetadataSource;import org.springframework.security.web.util.AntUrlPathMatcher;import org.springframework.security.web.util.UrlMatcher;import org.springframework.stereotype.Service;import com.rstco.frame.modules.orm.hibernate.HibernateDao;import com.rstco.frame.pub.security.da

31、o.PubAuthoritiesResourcesDao;import com.rstco.frame.pub.security.entity.PubAuthorities;import com.rstco.frame.pub.security.entity.PubResources;/* * * 最核心的地方,就是提供某個資源對應的權限定義,即getAttributes方法返回的結果。 * 注意,我例子中使用的是AntUrlPathMatcher這個path matcher來檢查URL是否與資源定義匹配, * 事實上你還要用正則的方式來匹配,或者自己實現(xiàn)一個matcher。 * * 此類在初

32、始化時,應該取到所有資源及其對應角色的定義 * * 說明:對于方法的spring注入,只能在方法和成員變量里注入, * 如果一個類要進行實例化的時候,不能注入對象和操作對象, * 所以在構造函數里不能進行操作注入的數據。 */Servicepublic class MyInvocationSecurityMetadataSourceService implementsFilterInvocationSecurityMetadataSource Autowiredprivate PubAuthoritiesResourcesDao pubAuthoritiesResourcesDao;priva

33、te UrlMatcher urlMatcher = new AntUrlPathMatcher();private static MapString, Collection resourceMap = null;public MyInvocationSecurityMetadataSourceService() loadResourceDefine();/* private void loadResourceDefine() resourceMap = new HashMapString, Collection(); Collection atts = new ArrayList(); Co

34、nfigAttribute ca = new SecurityConfig(ROLE_ADMIN); atts.add(ca); resourceMap.put(/index.jsp, atts); resourceMap.put(/i.jsp, atts); */private void loadResourceDefine() ApplicationContext context = new ClassPathXmlApplicationContext(applicationContext.xml);SessionFactory sessionFactory = (SessionFacto

35、ry)context.getBean(sessionFactory);Session session = sessionFactory.openSession();List query=session.createSQLQuery(select authority_name from pub_authorities ).list();resourceMap = new HashMapString, Collection();Collection atts = new ArrayList();/List auths =session.createQuery(arg0); /pubAuthorit

36、iesResourcesDao.findAuthAll(); for (String auth : query) ConfigAttribute ca = new SecurityConfig(auth);/ ROLE_ADMIN/ atts.add(ca);List query1=session.createSQLQuery(select resource_string +from Pub_Authorities_Resources,Pub_Resources, Pub_authorities +where Pub_Authorities_Resources.resource_id=Pub_

37、Resources.resource_id and + Pub_Authorities_Resources.resource_id=Pub_authorities.authority_id and + Authority_name=+auth+).list();for (String res : query1) String url = res;/ 判斷資源文件和權限的對應關系,如果已經存在,要進行增加if (resourceMap.containsKey(url) Collection value = resourceMap.get(url);value.add(ca);resourceMa

38、p.put(url, value);/ log.jsp,role_user,role_admin else atts.add(ca);resourceMap.put(url, atts); resourceMap.put(url, atts);/ According to a URL, Find out permission configuration of this URL.public Collection getAttributes(Object object)throws IllegalArgumentException / guess object is a URL.String u

39、rl = (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 getA

40、llConfigAttributes() return null;最重要的是decide方法,如果不存在對該資源的定義,直接放行;否則,如果找到正確的角色,即認為擁有權限,并放行,否則throw new AccessDeniedException(no right);這樣,就會進入上面提到的403.jsp頁面。(MyAccessDecisionManager)代碼如下:package com.rstco.frame.pub.security.support;import java.util.Collection;import java.util.Iterator;ccess.AccessDec

41、isionManager;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.springf

42、ramework.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 configura

43、tion 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 configAttributes) thro

44、ws AccessDeniedException, InsufficientAuthenticationException if(configAttributes = null) return ; System.out.println(object.toString(); /object is a URL. Iterator ite=configAttributes.iterator(); while(ite.hasNext() ConfigAttribute ca=ite.next(); String needRole=(SecurityConfig)ca).getAttribute();

45、for(GrantedAuthority ga:authentication.getAuthorities() if(needRole.equals(ga.getAuthority() /ga is users role. return; throw new AccessDeniedException(no right); public boolean supports(ConfigAttribute attribute) / TODO Auto-generated method stub return true; public boolean supports(Class clazz) re

46、turn true; 這個過濾器要插入到授權之前。最核心的代碼就是invoke方法中的InterceptorStatusToken token = super.beforeInvocation(fi);這一句,即在執(zhí)行doFilter之前,進行權限的檢查,而具體的實現(xiàn)已經交給accessDecisionManager了(MyFilterSecurityInterceptor)代碼如下:package erceptor;eption;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.beans.factory.annotation.Autowired;import org.springf

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論