2012-08-13 106 views
41

我希望每個請求都能收到一些信息,所以我認爲不是每個請求都有一個函數,並分別從請求中獲取這些信息,最好是有一個過濾器。
因此,每個請求都應該通過該過濾器,並獲得我想要的。


問題是:如何編寫自定義過濾器?
假設它不像任何預定義的彈簧安全過濾器,它是全新的。如何在彈簧安全中編寫自定義過濾器?

回答

42

您可以使用標準的Java過濾器。只需將它放在web.xml中的認證過濾器之後(這意味着它將在後面的過濾器鏈中並將在安全過濾器鏈之後調用)。

public class CustomFilter implements Filter{ 

    @Override 
    public void destroy() { 
     // Do nothing 
    } 

    @Override 
    public void doFilter(ServletRequest req, ServletResponse res, 
      FilterChain chain) throws IOException, ServletException { 

      HttpServletRequest request = (HttpServletRequest) req; 

      Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 

      Set<String> roles = AuthorityUtils.authorityListToSet(authentication.getAuthorities()); 
      if (roles.contains("ROLE_USER")) { 
       request.getSession().setAttribute("myVale", "myvalue"); 
      } 

      chain.doFilter(req, res); 

    } 

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

} 

片段的web.xml:

<!-- The Spring Security Filter Chain --> 
<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> 

<!-- Your filter definition --> 
<filter> 
    <filter-name>customFilter</filter-name> 
    <filter-class>com.yourcompany.test.CustomFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>customFilter</filter-name> 
    <url-pattern>/VacationsManager.jsp</url-pattern> 
</filter-mapping> 

你也可以添加處理程序,將全成登錄後調用(您需要擴展SavedRequestAwareAuthenticationSuccessHandler)。 Look here如何做到這一點。我認爲這是更好的主意。


更新:
或者你可以在你的安全過濾器這樣的結尾有這樣的過濾器:

<security:filter-chain-map> 
    <sec:filter-chain pattern="/**" 
      filters=" 
     ConcurrentSessionFilterAdmin, 
     securityContextPersistenceFilter, 
     logoutFilterAdmin, 
     usernamePasswordAuthenticationFilterAdmin, 
     basicAuthenticationFilterAdmin, 
     requestCacheAwareFilter, 
     securityContextHolderAwareRequestFilter, 
     anonymousAuthenticationFilter, 
     sessionManagementFilterAdmin, 
     exceptionTranslationFilter, 
     filterSecurityInterceptorAdmin, 
     MonitoringFilter"/> <!-- Your Filter at the End --> 
</security:filter-chain-map> 

而你的過濾器,你可以使用這個:

public class MonitoringFilter extends GenericFilterBean{ 
@Override 
public void doFilter(ServletRequest request, ServletResponse response, 
     FilterChain chain) throws IOException, ServletException { 
    //Implement this Function to have your filter working 
} 
+0

當我在等待答案時,我想出了一個解決方案。我在我的安全過濾器的末尾添加了我的過濾器(擴展'GenericFilterBean')。它工作得很好。但現在,當我看到你的答案時,對我來說聽起來更好。我正在嘗試解決您的問題。希望它也能起作用。我會讓你知道結果。謝謝。 – 2012-08-13 07:05:55

+0

通常你的回答是正確的,但我必須修改一下。謝謝你的幫助。 – 2012-08-13 07:25:52

+0

好的。修改我的權限,我會接受你的修改。我很有興趣知道我錯在哪裏。 – dimas 2012-08-13 07:31:42

13

Just把這個混在一起; 內部使用custom-filter怎麼樣:

<security:http auto-config="false" ...> 
    ... 
    <security:custom-filter position="FORM_LOGIN_FILTER" ref="MyCustomFilter" /> 
</security:http> 
+1

這是迄今爲止最好的解決方案。我想在所有其他方面運行我自己的過濾器: 2016-12-01 21:44:36

相關問題