2017-11-01 5 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으로 리디렉션됩니다.

无效账号和密码
你已注销
<형태 일 : 행동 = "@ {/ 로그인}"방법 = "POST">

답변

0

당신은 어떻게 행동을해야합니까? 기본적으로 /index과 같이 정적 인 정적 인 잘 알려진 위치로 리디렉션하거나 원래 요청한 페이지로 리디렉션하는 두 가지 옵션이 있습니다. 둘 다 AuthenticationSuccessHandler을 구성해야합니다. 기존의 인증 핸들러 중 하나를 사용/확장하여 몇 가지 기본 태스크를 수행 할 수도 있습니다. 예컨대, 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으로 다시 리디렉션됩니다. –