2016-07-19 8 views
0

私は自分のプロジェクトで春のセキュリティを使用しています。私はコードからユーザーを保存します。ユーザがログインしてログインページを開こうとした場合、どうすればメインページを開くことができますか?(春のセキュリティ)

@Override 
    public void saveUser(AuthLkUser lkUser) { 
     final List<GrantedAuthority> grantedAuths = new ArrayList<>(); 
     GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("ROLE_USER"); 
     grantedAuths.add(grantedAuthority); 
     UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(lkUser.getMsisdn(), lkUser.getPricePlan(), grantedAuths); 
     SecurityContextHolder.getContext().setAuthentication(result); 
    } 

正常に機能します。しかし、succsessufullログイン後、再びログインページを開くことができます。 succsessufullログイン後にオープン可能なログインページを無効にする必要があります。私はこれを試みた:

.antMatchers("/login", "/default/login").access("hasRole('ANONYMOUS')") 

しかし、 "ユーザー"の役割を持つユーザーもログインページを開くことができます。

私は

@Autowired 
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler; 

.successHandler(myAuthenticationSuccessHandler) 

しかし、このメソッドが呼び出されないsuccsessufullログイン後の設定へ

@Component 
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler { 
    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication auth) throws IOException, ServletException { 
     // initialization logic after login 

     // redirect 
     HttpSession session = request.getSession(); 
     SavedRequest savedReq = (SavedRequest) session.getAttribute("SAVED_REQUEST"); 
     if (savedReq == null) { 
      response.sendRedirect(request.getContextPath() + "/landing"); 
     } 
     else { 
      response.sendRedirect(savedReq.getRedirectUrl()); 
     } 
    } 
} 

とセットアップを試してみました。

ユーザーがログインしてログインページを開こうとした場合、メインページを開くにはどうすればよいですか?

なぜ私のmyAuthenticationSuccessHandlerは呼び出されませんか?

それは、私はあなたがすでにユーザがログインしているかどうかログイン要求をチェックする必要のあるコントローラを追加する必要があると思う私の設定

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(securedEnabled = true) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler; 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.authorizeRequests() 
       .antMatchers("/", "/index").access("hasRole('USER')") 
//    .antMatchers("/login", "/default/login").access("hasRole('ANONYMOUS')") 
       .and() 
       .formLogin() 
       .loginPage("/login") 
       .loginProcessingUrl("/j_spring_security_check") 
       .successHandler(myAuthenticationSuccessHandler) 
       .permitAll(); 
     http.csrf() 
       .disable() 
       .authorizeRequests() 
       .antMatchers("/resources/**", "/**").permitAll() 
       .anyRequest().permitAll() 
       .and(); 
    } 
} 

答えて

0

です。ログインページを表示する以外にログインしていない場合は、ホームページにリダイレクトします。続いて、このようなサンプルコントローラです:

@Controller 
@RequestMapping("/") 
public class IndexController { 

    @RequestMapping(value="/login",method = RequestMethod.GET) 
    public String index(){ 

     if(SecurityUtils.isUserLoggedIn()) 
      return "redirect:/home"; 

     return "login"; 

    } 

} 

そして次は、ユーザーがかログインしているかどうかを確認するためのユーティリティクラス有する方法であるサンプルSecurityUtils.javaクラスである:私は正確に何

public class SecurityUtils { 

    public static boolean isUserLoggedIn(){ 
     Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); 
     if(null != authentication){ 
      return (authentication.isAuthenticated() && !(authentication instanceof AnonymousAuthenticationToken)); 
     }else{ 
      return false; 
     } 

    } 
} 
+0

。しかし、私はそれが悪い習慣だと思う – user5620472

関連する問題