2012-03-08 82 views

回答

5

有一個在Spring Security FAQ一個非常類似的問題。

你可以注入定製AuthenticationDetailsSource到認證過濾器來提取傳入的請求的其他相關信息。然後可以從提交的Authentication對象中獲取該信息,該定製對象爲AuthenticationProvider

+4

其實答案就簡單多了。看到這個問題:http://stackoverflow.com/questions/7838808/spring-security-retrieve-user-ip-browser-info-and-requested-page – cowls 2014-07-10 14:53:00

0

你需要做的servlet一個Spring bean描述here

3

一個可能的解決方案是使用RequestContextFilter。您可以在web.xml將其定義爲在下面的代碼片段:

<filter> 
    <filter-name>requestContextFilter</filter-name> 
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>requestContextFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<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> 

,或者如果你只需要它的一些安全問題,那麼更好的地方是把它放到春季安全配置文件,如在下面的例子中:

<?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.1.xsd 
         http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd"> 

    <http> 
    <custom-filter ref="requestContextFilter" before="FORM_LOGIN_FILTER"/> 
    <form-login login-page="/login" authentication-failure-url="/login?error=failed" /> 
    </http> 
    <beans:bean id="requestContextFilter" class="org.springframework.web.filter.RequestContextFilter"/> 

    <authentication-manager alias="authManager"> 
    <authentication-provider ref="authProvider" /> 
    </authentication-manager> 
    <beans:bean id="authProvider" class="my.company.CustomAuthProvider" /> 
</beans:beans> 

然後你可以使用RequestContextHolder.currentRequestAttributes()方法Spring Security的類。舉例如下:

public class CustomAuthProvider extends DaoAuthenticationProvider { 
    @Override 
    public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
    ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); 
    System.err.println(attr.getRequest().getParameter("myParameterName")); 
    return super.authenticate(authentication); 
    } 
} 
相關問題