2016-07-25 34 views
0

目前我正在創建沒有登錄頁面的網頁。如何驗證用戶在春季安全中使用信息頭信息

我還有一個網站,將發送一個標頭信息:

user:John 
userCode:1234567 

所以我目前的網站將檢查頭的內容和驗證的認證管理器的用戶是這樣的:

首先我創建AuthenticationEntryPoint,這樣未認證的用戶就會去那裏。在AuthenticationEntryPoint我創建一個令牌並將用戶重定向到主頁面,所以在它進入主頁面之前,spring會對用戶進行身份驗證併爲有效用戶提供令牌這一頁。該代碼是這樣的:

@Component 
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint { 
     if(authException.getClass().getSimpleName().equals("InsufficientAuthenticationException")) { 
      if (request.getHeader("user") != null) { 
       UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(request.getHeader("user"), request.getHeader("userCode")); 
       SecurityContextHolder.getContext().setAuthentication(auth); 
       response.sendRedirect(request.getContextPath()); 
      } 
     } 
} 

AuthenticationManager處理則像往常一樣,給的令牌,如果用戶是有效的。有什麼我需要改變或可以在春季使用的另一種方法?

謝謝!

+0

我不知道預認證應該這樣處理,我指的是自定義的AuthenticationEntryPoint。請參閱參考文檔:[Http403ForbiddenEntryPoint](http://docs.spring.io/spring-security/site/docs/4.0.x/reference/html/preauth.html#http403forbiddenentrypoint) –

回答

1

您的情況使我想到了參考文檔中的Siteminder implementation example。 使用Siteminder,標頭(SM_USER)與HTTP請求一起傳遞。

這是Spring Security中預認證的一個例子。

你試過這個配置嗎? 他們首先定義了一個「自定義過濾器」,它是一個實例RequestHeaderAuthenticationFilter

提取的文件:

<security:http> 
<!-- Additional http configuration omitted --> 
<security:custom-filter position="PRE_AUTH_FILTER" ref="siteminderFilter" /> 
</security:http> 

<bean id="siteminderFilter" class="org.springframework.security.web.authentication.preauth.RequestHeaderAuthenticationFilter"> 
<property name="principalRequestHeader" value="SM_USER"/> 
<property name="authenticationManager" ref="authenticationManager" /> 
</bean> 

<bean id="preauthAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider"> 
<property name="preAuthenticatedUserDetailsService"> 
    <bean id="userDetailsServiceWrapper" 
     class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper"> 
    <property name="userDetailsService" ref="userDetailsService"/> 
    </bean> 
</property> 
</bean> 

<security:authentication-manager alias="authenticationManager"> 
<security:authentication-provider ref="preauthAuthProvider" /> 
</security:authentication-manager> 
+0

謝謝,我已經操縱你的答案,以適應與我的情況。 – FreezY