2017-11-01 28 views
2

私は春のブートで新しく、私は小さなアプリケーションで春のブートと春のセキュリティを使用しています。ログインに成功すると、ページは/ loginに再度リダイレクトされます。私はそれを修正する方法を知らない。ログインに成功すると、URLは/ loginに再度リダイレクトされます

ログイン成功後:

enter image description here

これは、セキュリティの設定:

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{ 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.csrf().disable() 
       .authorizeRequests() 
       .antMatchers("/", "/login").permitAll()//设置SpringSecurity对"/"和"/login"路径不拦截 
       .anyRequest().authenticated() 
       .and() 
       .formLogin() 
       .loginPage("/login")//设置Spring Security的登录页面访问路径为/login 
       .defaultSuccessUrl("/chat")//登录成功后转向/chat路径 
       .permitAll() 
       .and() 
       .logout() 
       .permitAll(); 


    } 

    /** 
    * 在内存中分别配置两个用户xin.luo和king.luo,密码和用户名一致,角色是USER 
    * @param auth 
    * @throws Exception 
    */ 
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
       .inMemoryAuthentication() 
       .withUser("xin").password("xin").roles("USER") 
       .and() 
       .withUser("king").password("king").roles("USER"); 
    } 

    /** 
    * /resources/static/目录下的静态资源文件,Spring Security不拦截 
    * @param web 
    * @throws Exception 
    */ 
    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring().antMatchers("/resources/static/**"); 
    } 
} 
+1

を使用すると、クライアント側では何がありますか?認証が成功したかどうかは確かに分かりますか? –

+0

ここに私のログインページがあり、認証が成功したと思います。しかし、ページは上の図のように、/ loginから/ chatにリダイレクトされた後に再び/ loginにリダイレクトされます。

无效账号和密码
你已注销
<形番:アクション= "@ {/ログイン}" メソッド= "ポスト">

答えて

0

あなたはどのような行動が必要なのでしょうか?基本的には、2つの選択肢があります:/indexのようなスタティック静的でよく知られた場所にリダイレクトするか、最初に要求されたページにリダイレクトします。両方ともAuthenticationSuccessHandlerを設定する必要があります。また、既存の認証ハンドラの1つを使用/拡張して、基本的なタスクを実行することもできます。例えば、SimpleUrlAuthenticationSuccessHandlerは元々要求されたページにリダイレクトするために使用することができますどのように注意してください。

XML Secutiryの設定:

<http use-expressions="true"> 
    <intercept-url pattern="/login*" access="permitAll"/> 
    <intercept-url pattern="/**" access="isAuthenticated()"/> 

    <form-login 
     ... 
     authentication-success-handler-ref="authenticationSuccessHandler" 

     authentication-success-handler-ref="refererAuthenticationSuccessHandler" 
     ... 
     /> 

    <logout/> 
</http> 

<!-- Route users to their profiles and admins to the admin console: --> 
<beans:bean id="authenticationSuccessHandler" class="a.b.c.AuthenticationSuccessHandler"/> 

<!-- Route to the originally requested page --> 
<beans:bean id="refererAuthenticationSuccessHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler"> 
    <property name="useReferer" value="true"/> 
</beans:bean> 

AuthenticationSuccessHandler

public class AuthenticationSuccessHandler implements AuthenticationSuccessHandler { 
    @Override 
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException { 
     // Very simple (most probably broken) check if the user is ADMIN or USER 
     if (authentication.getAuthorities().stream().filter(a -> a.getAuthority().equals("USER")).findAny() != null){ 
      redirectStrategy.sendRedirect(request, response, "/profile.html"); 
     } else { 
      redirectStrategy.sendRedirect(request, response, "/admin.html"); 
     } 

     clearAuthenticationAttributes(request); 
    } 
} 
+0

あなたの助けをいただき、ありがとうございます。このアプリケーションでは、ログインが成功した後に/ loginから/ chatにページをリダイレクトしたいと思います。私はWebSecurityConfig.javaでデフォルトの成功URLを設定しましたが、上記の図のように/ chatにページをリダイレクトしてから/ loginに再度リダイレクトしました。 –

関連する問題