隨筆-193  評論-715  文章-1  trackbacks-0
          本Blog所有內(nèi)容不得隨意轉(zhuǎn)載,版權(quán)屬于作者所有。如需轉(zhuǎn)載請與作者聯(lián)系( fastzch@163.com    QQ:9184314)。
          未經(jīng)許可的轉(zhuǎn)載,本人保留一切法律權(quán)益。


          Spring Security 3.x 出來一段時間了,跟Acegi是大不同了,與2.x的版本也有一些小小的區(qū)別,網(wǎng)上有一些文檔,也有人翻譯Spring Security 3.x的guide,但通過閱讀guide,無法馬上就能很容易的實現(xiàn)一個完整的實例。

          我花了點兒時間,根據(jù)以前的實戰(zhàn)經(jīng)驗,整理了一份完整的入門教程,供需要的朋友們參考。
          1,建一個web project,并導(dǎo)入所有需要的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>
          這個文件中的內(nèi)容我相信大家都很熟悉了,不再多說了。

          2,來看看applicationContext-security.xml這個配置文件,關(guān)于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"><!-- 當訪問被拒絕時,會轉(zhuǎn)到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三個屬性,
              我們的所有控制將在這三個類中實現(xiàn),解釋詳見具體配置 
          -->
              
          <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>
              
              
          <!-- 認證管理器,實現(xiàn)用戶認證的入口,主要實現(xiàn)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" />

              
          <!-- 訪問決策器,決定某個用戶具有的角色,是否有足夠的權(quán)限去訪問某個資源 -->
              
          <beans:bean id="myAccessDecisionManagerBean"
                  class
          ="com.robin.erp.fwk.security.MyAccessDecisionManager">
              
          </beans:bean>
              
              
          <!-- 資源源數(shù)據(jù)定義,即定義某一資源可以被哪些角色訪問 -->
              
          <beans:bean id="securityMetadataSource"
                  class
          ="com.robin.erp.fwk.security.MyInvocationSecurityMetadataSource" />

          </beans:beans>

          3,來看看自定義filter的實現(xiàn):
          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);這一句,即在執(zhí)行doFilter之前,進行權(quán)限的檢查,而具體的實現(xiàn)已經(jīng)交給accessDecisionManager了,下文中會講述。

          4,來看看authentication-provider的實現(xiàn):
          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;
              }

              
          }
          在這個類中,你就可以從數(shù)據(jù)庫中讀入用戶的密碼,角色信息,是否鎖定,賬號是否過期等,我想這么簡單的代碼就不再多解釋了。

          5,對于資源的訪問權(quán)限的定義,我們通過實現(xiàn)FilterInvocationSecurityMetadataSource這個接口來初始化數(shù)據(jù)。
          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;

          /**
           * 
           * 此類在初始化時,應(yīng)該取到所有資源及其對應(yīng)角色的定義
           * 
           * 
          @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角色的用戶才能訪問。
          這個類中,還有一個最核心的地方,就是提供某個資源對應(yīng)的權(quán)限定義,即getAttributes方法返回的結(jié)果。注意,我例子中使用的是AntUrlPathMatcher這個path matcher來檢查URL是否與資源定義匹配,事實上你還要用正則的方式來匹配,或者自己實現(xiàn)一個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方法,如果不存在對該資源的定義,直接放行;否則,如果找到正確的角色,即認為擁有權(quán)限,并放行,否則throw new AccessDeniedException("no right");這樣,就會進入上面提到的403.jsp頁面。

          參考資料:
          1,Spring官方網(wǎng)站:http://www.springframework.org
          2,文章所用的代碼,MyEclipse工程,去掉了lib,請自行下載Spring Security 3.x的包,并copy至對應(yīng)目錄。工程源代碼
          3,根據(jù)網(wǎng)絡(luò)上的資料,制作的CHM版的 Spring Security 3.x 參考手冊中文版
          4,2009年3月,我在“IBM WebSphere技術(shù)專家沙龍(華南區(qū)廣州站)”演講時的PPT:《Spring Security--Protect your web application》,當時是Spring Security 2.x,很多原理是一樣,可作參考。

          教程中為了盡可能不跟其它框架關(guān)聯(lián)上,所以去掉了訪問數(shù)據(jù)庫的部分,比如用戶信息和資源配置信息的讀取,直接寫死在代碼中了,大家可以根據(jù)自己的實際情況補充完整。

          如有任何疑問,歡迎大家以評論的方式提問,也歡迎大家討論!
          posted on 2010-03-10 12:38 Robin's Programming World 閱讀(59173) 評論(67)  編輯  收藏 所屬分類: Java

          評論:
          # re: Spring Security 3.x 完整入門教程 2010-03-12 10:23 | 看你想吐
          是在看不出來,哪里像3.x,3.x的精髓部分根本就沒有說明,入門教程有點淺,你這個頂多算是一個小實驗而已。
          放心吧,你不會在任何地方看見轉(zhuǎn)載的。  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-03-12 13:38 | 樓上你我看你想拉屎
          樓上的你看誰的想吐?你P能出個例子出來瞧瞧?標準的人型J8,就張了一張P嘴?標準的嘴子貨,在這唧唧歪歪的,不想學(xué)習(xí)的滾?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-03-14 09:57 | Messenger
          學(xué)習(xí)了?。。?nbsp; 回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-03-15 12:30 | 藍劍
          已經(jīng)能夠顯示出3.x和2.x的不同了,學(xué)習(xí)了  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-03-16 10:30 | 看你想吐
          @樓上你我看你想拉屎
          自古二樓出傻B,這一定律仍在繼續(xù),我的ss3你根本就看不懂,我貼出來你還上火,這樣的文章你也來頂,你能找到工作,我給你開資,垃-圾。  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-03-19 09:18 | 看你想吐就吐
          向你這種隨意踐踏別人的文章,不尊重別人勞動成果,只會寫一篇別人都看不懂的ss3的人...我可以替你媽媽告訴你:你媽媽叫你回家去吐...  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-03-28 18:12 | 第二次看了色
          @看你想吐就吐
          springsecurity3真是好東西,不過他完全寫到點上,還叫什么完整,居然還發(fā)布誤導(dǎo)人,還不讓轉(zhuǎn)載,要說別的我到不說話了,說道springsecurity我從acegi0.x版本就一直用,我都不敢輕易的發(fā)一些完整這類話的文章,就看你對ss3的理解,你就是一個入門尚淺的低級用戶,還敢說完整。想出名,想教別人,必須自己要會很多,付出很多,否則會貽笑大方的。  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-03-29 14:03 | 大江東流
          恩,雖說文章不是很好,看不出那里是實踐得來的,倒像是helloworld,不過入門的話,也許能行吧?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-03-30 12:11 | Robin's Java World
          看了大家的評論,覺得自己應(yīng)該更加努力,研究好Spring Security來幫助大家提高。
          SS確實是個好東西,我們曾經(jīng)在多個項目里不只一次的使用過,也是從最初的Acegi開始,并且實現(xiàn)了很多電信級的需求,比如登錄3次失敗鎖定賬號,24小時自動解鎖,不允許同一帳號同時在多個客戶端登錄等。  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2010-04-10 08:52 | roypayne
          看了前面的評論,真不敢恭維。
          都是搞技術(shù)的,還這么浮躁,有意思嘛。
          你覺得不怎么樣,可以不說風(fēng)涼話嗎?
          有時間,有這個精力,你怎么不拿出一篇文章來,又貼圖,又碼字的。

          鼓勵一下樓主,謝謝分享!  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-04-12 16:12 | 來如風(fēng)
          @看你想吐
          你認為核心在那里呢,樓主標題說了,"入門" 你丫就不能看看這兩個字嗎!!  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-04-14 14:33 | Arnold
          非常好,如果能有個demo下載就好了  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-04-20 09:18 | Robin's Java World
          @Arnold
          文章的參考資料里就有demo呀。  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-05-17 10:23 | sen
          你好
          部屬了你的代碼,我把resourceMap.put("/i.jsp", atts);注釋掉后發(fā)現(xiàn)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
          小弟不才,經(jīng)多次測試還是沒看到想看到的403頁面?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-05-28 12:40 | Ganky
          不錯的東西~~我初學(xué)Spring Security,你這篇文章給我很大的幫助!
          不過有個問題,你的pathMatchesUrl方法,兩個參數(shù)搞反了……
          讓我糾結(jié)了一個早上

          最后還是感謝你的貢獻  回復(fù)  更多評論
            
          # 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;
          }
          /**
          *采用有參數(shù)的構(gòu)造函數(shù)
          *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 配置修改如下
          資源源數(shù)據(jù)定義,即定義某一資源可以被哪些角色訪問
          -->
          <beans:bean id="securityMetadataSource"
          class="com.shoesishow.security.MyInvocationSecurityMetadataSource">
          <beans:constructor-arg><beans:ref bean="roleService"/></beans:constructor-arg>
          </beans:bean>  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-06-25 18:10 | chen3975
          上面的是通過動態(tài)讀取數(shù)據(jù)庫來實現(xiàn)的.請結(jié)合自身情況改改.其實樓主已經(jīng)把例子寫的挺好的了.  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-06-26 17:55 | luckyzhang
          FilterInvocationSecurityMetadataSource的實現(xiàn)中,如果需要注入獲取數(shù)據(jù)庫資源的dao,會提示:java.lang.NullPointerException,這點樓主并沒有解決。而這點通常是很重要的,如果僅僅是hardcode,直接在配置文件中寫都ok了,希望樓主可以解答。不要告訴我用jdbc直接取,這種代碼不優(yōu)雅!  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-06-26 17:56 | luckyzhang
          最近做項目,在這里卡住了,希望樓主見貼答復(fù),謝謝!qq:4639966, mail:cngdzql#gmail.com  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-06-28 09:51 | chen3975
          @luckyzhang
          我不是給你解決辦法了.
          就是采用有參數(shù)的構(gòu)造方法來解決注入你要的屬性例如:
          public MyInvocationSecurityMetadataSource(RoleService roleService) {
          this.roleService = roleService;
          loadResourceDefine();
          }
          RoleService 是可以獲得資源列表的組件.
          在xml配置文件中采用構(gòu)造函數(shù)注入的方式把RoleService 注入到MyInvocationSecurityMetadataSource中.例如
          <beans:bean id="securityMetadataSource"
          class="com.shoesishow.security.MyInvocationSecurityMetadataSource">
          <beans:constructor-arg><beans:ref bean="roleService"/></beans:constructor-arg>
          </beans:bean>
          我上面寫的很清楚.  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-07-04 23:07 | roamer
          @chen3975
          你這個使用dao的方法也挺好。但是如果能在MyInvocationSecurityMetadataSource類里面直接使用@Inject方法,就更符合非配置的annonation方法來。不知道有沒有解決辦法  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-08-18 21:15 | 感謝樓主
          真感謝樓主提供這么好的技術(shù)文獻,解決了小弟的一大難題!
          期待樓主貢獻更多的Spring-Security3的技術(shù)文獻!
          謝謝!  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2010-08-20 18:48 | Leo
          我也看不到403頁面,而且名字為空的話彈不出js腳本,很奇怪的問題

          隨意改變用戶名也可以正常登錄……怎么會這樣呢?  回復(fù)  更多評論
            
          # 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的

          不能自定義么?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-08-28 19:39 | 感謝樓主
          MyInvocationSecurityMetadataSource類里的 resourceMap 為什么要定義為static ?
          請不我不設(shè)置為 static 可以嗎 ?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2010-09-06 16:13 | 呵呵
          有代碼嗎?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-09-06 19:53 | yuxuan
          正在學(xué)習(xí)spring security,非常感謝您的教程!  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-09-12 17:50 | Robin's Java World
          @呵呵
          請見參考資料2  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-09-19 19:09 | hermes
          貌似有點問題,訪問的url不在定義的訪問內(nèi),還能訪問?不知道問題出在哪里了?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-09-19 20:30 | hermes
          MyAccessDecisionManager
          我咋發(fā)現(xiàn)當url匹配的時候才調(diào)用這個。。。而且沒有定義的照樣可以訪問~~
          樓主沒有遇到這個問題嗎?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-09-19 21:11 | hermes
          如果不存在對該資源的定義,直接放行;
          暈,沒有看到這句話。。。在問一句:
          spring security支持角色繼續(xù)嗎?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-09-26 08:34 | Robin's Java World
          @hermes
          當然支持角色。  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2010-09-28 15:59 | 難得糊涂
          lz水平確實一般,技術(shù)水平與文章功底比較弱,只是從自己角度解釋問題;
          自己學(xué)習(xí)總結(jié)倒也挺好,但是什么開篇一些不得轉(zhuǎn)載裝大拿的話太惹人嫌,比較膚淺,在別人看來如跳梁小丑一般啊。。。  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2010-09-28 17:53 | 天道酬勤
          請問下 ,在MyInvocationSecurityMetadataSource中,能不能通過spring的標注注入對象,然后使用比如。
          @Autowired
          private FunctionManager functionManager;

          但是我的怎么是空指針?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2010-12-27 01:29 | 越王夠賤
          @天道酬勤 我的也出了空指針啊!不知道怎么回事你搞懂了么
            回復(fù)  更多評論
            
          # 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;
          }
            回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2011-04-18 15:51 | ajaxljk
          org.springframework.security.web.util.UrlMatcher
          這個接口在spring-security3.1里面沒有了,是怎么回事,有它的替代接口嗎?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2011-05-05 09:24 | junxy
          這個問題我也遇到了 不存在資源定義時根本就不會走到decide方法里面去,不知道大家遇到?jīng)]?@hermes
            回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2011-07-25 09:14 | 草i你媽
          還惹上管事

          垃圾一個  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2011-07-25 09:18 |
          2009年3月,我在“IBM WebSphere技術(shù)專家沙龍(華南區(qū)廣州站)”演講時的PPT


          我暈 鳥人 知道你是那種人了
          講ppt的人了 就知道注重面子工程 寫過代碼沒?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2011-08-11 15:35 | one new worker
          樓主,很多沒接粗過spring security的人,更需要你的幫助,配置spring security真的好麻煩,能不能給一份重頭開始的配置呢?能加一些配置過程中的圖片就更好了。建完web project后一步一步配置的操作,網(wǎng)上沒有見到過這樣類似的配置,要知道你少些幾句話會讓初學(xué)者走很多彎路的。
          感謝樓主?。?!

          xiefh2011@foxmail.com  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2011-11-30 12:32 |
          你好,最近看了你的這篇博客,可是我還是不明白如果加入SSH框架,并且要驗證密碼是都正確的話應(yīng)該怎么弄,還希望您不吝賜教,小弟萬分感謝。郵箱:4926664447@qq.com  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2011-11-30 15:32 | 頂樓主
          我是新人,以前從未弄過Spring Security,甚至今天之前還不知道Spring Security是個什么東東。 看了一遍樓主的這篇帖子后,表示還是不懂。希望樓主給點更詳細的,感謝  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2012-04-20 15:13 | S
          @看你想吐就吐
          SHIT  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2012-04-20 15:15 | S
          @樓上你我看你想拉屎
          SON FO BITCH  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2012-08-23 17:12 | Mr. Yang
          謝謝樓主分享,學(xué)習(xí)了  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2012-08-29 10:17 | fuck
          網(wǎng)上的垃圾已經(jīng)夠多了  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2012-10-09 16:47 | 無名
          樓主我問一下你都導(dǎo)入了哪些jar包  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2012-10-15 15:07 | barbarainboy
          不錯的帖子,對于我這樣的新手來說,真是如魚得水了~~~~~~~~~~~~~~~~~~~  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2013-01-24 15:39 | IT小鳥
          你好,我想請教一下403頁面的配置
          特別是在403中獲取throw new AccessDeniedException("no right");
          這個異常的 信息  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2013-04-18 23:45 |
          好好讀一讀  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2013-05-02 18:18 | JAKE
          例子很好,謝謝,但是其中有寫類,被spring 廢棄了,所以應(yīng)該有更好的示例。樓主無視那些噴子吧。  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2013-05-07 09:45 | 日明月易
          本人初學(xué)者,請問要導(dǎo)入哪些jar包啊,我導(dǎo)入了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包樓主能否貼出來,謝謝啦
            回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2013-06-12 23:32 | 蝸牛
          你這個是個包沖突,我前天也是這樣,后來換了包就好了,包沖突真的很蛋碎。。。@日明月易
            回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2013-08-16 10:15 | zzj
          @看你想吐就吐
          媽的,寫的代碼讓人容易理解那才叫高手,你Y的寫的SS3讓人看不懂,那叫啥???  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2013-09-17 14:46 | 過客
          看不懂別人寫的東西,就謙虛點,別人不欠你的! 要么離開??!
          尊重:這是做人最起碼的!  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2013-10-20 22:04 | 路過
          這個也叫完整教程  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2013-11-13 16:33 | 初學(xué)
          離帖子都幾年了。。。接觸Spring Security有3天了,一直沒有例子看看,這個不錯了,幫助很大
            回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2013-11-29 17:49 | Neddy
          <input type="text" name="xxx">  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2014-01-22 15:57 | name
          回帖里面有些看起來很牛逼的人,發(fā)個教程吧。  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2014-02-24 10:03 | 零度
          請問樓主按您的方法我的應(yīng)用報了:java.lang.IllegalArgumentException: AccessDecisionManager does not support secure object class: class org.springframework.security.web.FilterInvocation 這樣的錯。請問是什么問題怎么樣解決  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2014-10-10 10:55 | sailer
          請問樓主我的應(yīng)用報了:Caused by: java.lang.IllegalArgumentException: SecurityMetadataSource does not support secure object class: class org.springframework.security.web.FilterInvocation 網(wǎng)上搜了很久也沒有相關(guān)的答案,不知樓主能否給解答下。非常感謝!  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程[未登錄] 2014-10-10 12:28 | sailer
          我也遇到了這個錯誤,后面發(fā)現(xiàn)可能和決策器里面的supports函數(shù)有關(guān),默認返回false,改成返回true之后就能啟動了,哦 還有給資源授權(quán)的那個類里面的supports函數(shù)也返回true就可以了 @零度
          轉(zhuǎn)自http://edwin492.iteye.com/blog/1151789  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2014-12-26 17:26 | smile雪淚寒
          樓主有源碼嗎?麻煩發(fā)一份完整的Demo+sql文件給我,謝謝,1490020533 qq  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2015-05-28 10:38 | Jackie
          有個問題:角色和模塊的關(guān)系建立是在啟動的時候。一般的運行程序后臺添加關(guān)系,這樣新添加的關(guān)系就沒用了,這個如何解決?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2015-09-09 14:27 | iechenyb
          AntUrlPathMatcher這個類所在jar包是哪個啊,是spring-security-core-tiger這個嗎?  回復(fù)  更多評論
            
          # re: Spring Security 3.x 完整入門教程 2016-01-04 17:40 | bns
          @過客


          尊重人是最起碼的  回復(fù)  更多評論
            
          主站蜘蛛池模板: 中山市| 舞阳县| 东兴市| 娱乐| 布尔津县| 石嘴山市| 黔西县| 博兴县| 宜兰县| 柳河县| 中山市| 阳泉市| 长丰县| 揭东县| 类乌齐县| 呼玛县| 海门市| 竹山县| 义马市| 隆昌县| 雅江县| 昌都县| 青龙| 林芝县| 湖南省| 大冶市| 金山区| 深水埗区| 察隅县| 临朐县| 武乡县| 奉节县| 会昌县| 丹凤县| 郯城县| 同心县| 崇礼县| 炎陵县| 军事| 松江区| 东明县|