2011-08-11 16 views
5

私はSpringには新しく、ユーザ名とパスワードでユーザを認証したくないという要件があります。私はちょうどに応じてページを確保するために春のセキュリティを使用したいSpring 3.0セキュリティ - 認証による認証

  1. ユーザー名
  2. 役割

: ユーザーが認証されると、他のアプリケーションと私のアプリの詳細をfolloingで要求を取得することです要求内の役割 私はUserDetailServiceの作成について考えましたが、リクエストデータを追加するだけで、Springは依然として認証情報を要求します。 は、それから私は、次のようなものを書くことについて考えた:

public class UserLogin { 

/* 
@Resource(name = "userDetailsService") 
private UserDetailsService userDetailsService; 
*/ 

@Resource(name = "authenticationManager") 
private AuthenticationManager authenticationManager; 

public boolean login(UserEntity user) { 

    //UserDetails ud = userDetailsService.loadUserByUsername(username); 

    Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); 
    for (String role : user.getAuthorities()) { 
     authorities.add(new GrantedAuthorityImpl(role)); 
    } 

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(), authorities); 

    try { 
     Authentication auth = authenticationManager.authenticate(token); 

     SecurityContext securityContext = new SecurityContextImpl(); 

     // Places in ThredLocal for future retrieval 
     SecurityContextHolder.setContext(securityContext); 
     SecurityContextHolder.getContext().setAuthentication(auth); 

    } catch (AuthenticationException e) { 
     return false; 
    } 

    return true; 
} 
} 

私は正しい方向に向かっています。もしそうなら、spring-xmlで全体を構成する方法..

答えて

8

あなたは事前認証シナリオと呼ばれるものです。ここでは、アクセス権のみを認証し、認証アクセスは許可しないようにSpring Securityを設定しています。 http://static.springsource.org/spring-security/site/docs/3.0.x/reference/preauth.htmlを参照してください。ここでは、認証方式のUserPrincipalをグレープするためにAbstractPreAuthenticatedProcessingFilterを実装し、上記のようにUserDetailsServiceを実装する必要のある完全な設定を示します。

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans 
xmlns:security="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"> 

<security:global-method-security secured-annotations="enabled" /> 

<beans:bean id="preAuthenticatedProcessingFilterEntryPoint" class="org.springframework.security.web.authentication.Http403ForbiddenEntryPoint" /> 

<security:http auto-config="false" entry-point-ref="preAuthenticatedProcessingFilterEntryPoint"> 
    <security:custom-filter position="PRE_AUTH_FILTER" ref="myCustomPreAuthFilter" /> 
</security:http> 

<beans:bean id="myCustomPreAuthFilter" class="com.mypackage.MyCustomPreAuthFilter"> 
    <beans:property name="authenticationManager" ref="authenticationManager" /> 
</beans:bean> 

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

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

関連する問題