2017-08-16 8 views
0

私はSpringセキュリティとThymeleafでプロジェクトを進めています。私は基本的なSpring Securityの統合を持っています。Springセキュリティを使用してページをリダイレクトするときにユーザーのROLEを認識できません

SecurityConfig.java

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter{ 

    @Autowired 
    private DataSource dataSource; 

    @Autowired 
     public void configureGlobal (AuthenticationManagerBuilder auth) throws Exception 
     { 
     auth 
      .jdbcAuthentication() 
      .dataSource(dataSource); 
     } 

    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/login").permitAll() 
       .anyRequest().authenticated() 
       .and() 
      .formLogin() 
       .loginPage("/login") 
       .defaultSuccessUrl("/success", true) 
       .and() 
      .httpBasic(); 
     } 
} 

SecurityWebApplicationInitializer.java

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer 
{ 
    public SecurityWebApplicationInitializer(){ 
     super(SecurityConfig.class); 
    } 

} 

Controller.java

@Controller 
public class HomeController { 

    @RequestMapping(value = "/login", method = RequestMethod.GET) 
    public String loginPage(Model model) { 
     return "login"; 
    } 

    @RequestMapping("/success") 
    public String loginPageRedirect(HttpServletRequest httpServletRequest){ 

     if(httpServletRequest.isUserInRole("ROLE_ADMIN")) { 
      return "index1"; 
     } else if(httpServletRequest.isUserInRole("ROLE_USER")) { 
      return "index2"; 
     } else { 
      return "index3"; 
     } 
    } 
} 

ログインに成功すると、ユーザーはリダイレクトされますが、間違ったページになります。私のユーザはロールROLE_USERを持っていますが、方法loginPageRedirectは、index2でなければならないときにページindex3にリダイレクトしています。私のユーザー役割は認識できないと思う。どうやってやるの? loginPageRedirectに何かをパラメータとして追加してロールを認識する必要がありますか?

+0

あなたがチェックしてくださいだろう: '/ SUCCESS'にパラメータ'認証authentication'を追加しますコントローラメソッドでは、spring mvcが自動的に認証を挿入します。そして、このメソッドの中で、 'authentication.getAuthorities()'をすべて出力し、役割が正しいかどうかを確認してください。 – sunkuet02

+0

@ sunkuet02はい、このような役割[ROLE_USER]を表示します。しかし、私はまだ間違ったリダイレクトを持っています。 –

+0

'loginPageRedirect'メソッドの始めに' httpServletRequest.authenticate(response);を追加してみてください。このコントローラで追加のパラメータ 'HttpServletResponse response'を追加してください。 – sunkuet02

答えて

1

私に適したソリューションが見つかりました。

私はこのような私のloginPageRedirect方法、編集:

@RequestMapping("/success") 
    public void loginPageRedirect(HttpServletRequest request, HttpServletResponse response, Authentication authResult) throws IOException, ServletException { 

     String role = authResult.getAuthorities().toString(); 

     if(role.contains("ROLE_ADMIN")){ 
     response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/index1"));        
     } 
     else if(role.contains("ROLE_USER")) { 
      response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/index2")); 
     } 
    } 

が、それは同じ問題を持つ人がお役に立てば幸いです:)

+0

+1いいですね。 'Authentication'は後でそれを助けます。あなたの前の質問について私の最後の答えを確認することができます – sunkuet02

+0

@ sunkuet02ありがとうございます。 :)私はまた、その質問に完全な答えを掲載した。 –

関連する問題