2016-05-24 12 views
0

私は、認証にSpring MVCとSpring Securityを使用しています。ユーザー名とパスワードでログインしていますが、もう2つのパラメータを送信したいと思います。それを行う方法はありますか?複数のパラメータを持つSpringセキュリティログイン

これは、ログインフォームです:

<form action="#" th:action="@{/auth/login}" th:object="${loginBean}" method="post"> 
    <input type="hidden" th:field="*{timezone}" th:value="*{timezone}" /> 
    <input type="hidden" th:field="*{language}" th:value="*{language}" /> 
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" /> 
    <input type="text" placeholder="Email" th:field="*{username}" required="required" 
     title="Email pattern does not match" pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$"/> 
    <span th:if="${#fields.hasErrors('username')}" th:errors="*{username}">Email Error</span> 
    <input type="password" placeholder="Password" th:field="*{password}" required="required" /> 
    <span th:if="${#fields.hasErrors('password')}" th:errors="*{password}">Password Error</span> 
    <button type="submit" id="login-button">Login</button>      
</form> 

私もログインしてタイムゾーンと言語をお送りしたいと思います。

そしてAuthenticationProvider

@Component 
public class CustomAuthenticationProvider implements AuthenticationProvider { 

    @Override 
    public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
     String name = authentication.getName(); 

     // You can get the password here 
     String password = authentication.getCredentials().toString(); 

     TokenDTO t = new TokenDTO(); 
     t.setIdioma("en-US"); 

     //try { 

     CustomUserService userService = new CustomUserService(); 

     UserDetails user = userService.loadUser(name, password); 

     if (user == null) 
      throw new BadCredentialsException("Username not found."); 

     Collection<? extends GrantedAuthority> authorities = user.getAuthorities(); 

     Authentication auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(), authorities); 

     return auth;  
    } 

    @Override 
    public boolean supports(Class<?> authentication) { 
     return authentication.equals(UsernamePasswordAuthenticationToken.class); 
    } 
} 

それが認証によりのparamsを送信することは可能ですか?

+1

以下のリンクを参照してください: http://stackoverflow.com/questions/10074308/how-to-pass-an-additional-parameter-with-spring-security-login-page – nits

答えて

-1

カスタム認証を実装し、上記のように行う必要があります。

編集: CustomAuthenticationProviderでは、カスタム認証オブジェクトを返す必要があります。 Authenticationインターフェイスを実装するクラスを実装することができます。

+1

することができますあなたの解決策を詳しく解説してください。リンクを提供するのではなく、投稿のステップを説明してください。 OPは答えを得るためにリンクをクリックしてはならないはずです(リンクが下がってもあなたのアンカーはまだ関連しています) – litelite

1

1.YouカスタムフィルタCustomUsernamePasswordAuthenticationFilterを作成することができ、それは次のようになります。

public class CustomUsernamePasswordAuthenticationFilter extends UsernamePasswordAuthenticationFilter{ 

    @Override 
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { 
     final String languageParam = request.getParameter("language"); 
     request.getSession().setAttribute("language", languageParam); 
     System.out.println("Filter" + languageParam); 
     // You can do your stuff here 
     return super.attemptAuthentication(request, response); 
    } 
} 

2.Inセキュリティの設定カスタムフィルタのBeanを作成

@Bean 
public UsernamePasswordAuthenticationFilter authenticationFilter() { 
    CustomUsernamePasswordAuthenticationFilter authFilter = new CustomUsernamePasswordAuthenticationFilter(); 
    List<AuthenticationProvider> authenticationProviderList = new ArrayList<AuthenticationProvider>(); 
    authenticationProviderList.add(authenticationProvider); 
    AuthenticationManager authenticationManager = new ProviderManager(authenticationProviderList); 
    authFilter.setAuthenticationManager(authenticationManager); 
    authFilter.setUsernameParameter("username"); 
    authFilter.setPasswordParameter("password"); 
    return authFilter; 
} 

3.Inセキュリティ設定HTTP用のaddFilterを設定すると、次のようになります。

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http.authorizeRequests() 
    .antMatchers("/").permitAll() 
    .antMatchers("/home").access("hasRole('ROLE_USER')") 
    .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')") 
    .antMatchers("/api/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_API')") 
    .and() 
    .formLogin() 
    .loginProcessingUrl("/login") 
    .loginPage("/loginPage") 
    .failureUrl("/loginPage?error") 
    .defaultSuccessUrl("/home") 
    .usernameParameter("username") 
    .passwordParameter("password") 
    .and() 
    .exceptionHandling() 
    .accessDeniedPage("/Access_Denied").and() 
    .addFilterBefore(authenticationFilter(), UsernamePasswordAuthenticationFilter.class); 


} 

コントローラにこれらの追加値を追加する場合は、HttpServletRequestリクエストパラメータを追加し、このようなセッションから値を取得できます。

request.getSession().getAttribute("language"); 
+0

ありがとう!私はあなたのソリューションを試していますが、私があなたがSegurity Configで述べたようにlog.Iを追加しようとするとカスタムフィルタが呼び出されません。 – Zuri

+0

私はカスタムauthenticationProviderの実装でそれを評価し、それは私のために動作します、あなたはカスタムauthenticationProviderを提供する必要があるかもしれません。 –

関連する問題