成功登录后,URL再次重定向到/ login

Xin_Law:

我是Spring Boot的新手,我有一个使用Spring Boot和Spring Security的小应用程序。成功登录后,页面将再次重定向到/ login。我不知道该如何解决。

成功登录后:

在此处输入图片说明

这是安全性配置:

@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/**");
    }
}
疯子:

您需要什么行为?基本上,有两种选择:重定向到某个静态静态的知名位置(如)/index,或重定向到最初请求的页面。两者都需要配置AuthenticationSuccessHandler您还可以使用/扩展现有的身份验证处理程序之一来完成一些基本任务。例如,请注意如何SimpleUrlAuthenticationSuccessHandler用于重定向到最初请求的页面:

XML安全配置:

<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);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

登录成功后,Spring Oauth和安全性将重定向到/ login

登录后 /login 应重定向到主页

django login_required装饰器,即使登录后也始终重定向到登录页面

用户登录后 Flask-Login 重定向到 React 客户端

如果用户未登录Laravel,则重定向到Login

成功登录后 CAS 重定向到 URL

Laravel Ajax登录,成功后重定向到先前的URL

从CustomLogoutHandler重定向到'login?logout'被重定向到'login'

总是重定向到/ login

flask_login @login_required登录后继续将我重定向到登录页面

登录后,Spring Security再次重定向到登录页面

Laravel 5.4,如何在成功登录后将登录用户重定向到引荐URL?

Django:Login_required(重定向到自定义 login_url)不起作用

重定向到/ admin / login /结果302

AuthorizeAttribute不断重定向到/ Account / Login

为什么/ login?logout重定向到/ login?

将Active Admin登录名重定向到Devise Login

如果路径为“ /”或“ / login”,如何重定向到登录名?

Symfony2在未登录时将/ admin / *重定向到/ admin / login(或使用FOSUserBundle?)

为什么django allauth重定向到/ accounts / login /?= next / MY_PAGE而不是登录?

Angular2-成功登录后重定向到调用URL

成功登录后获取文件下载而不是重定向到默认 URL

Django Rest Framework / AngularJS-即使我在settings.py中设置了LOGIN_REDIRECT_URL,用户登录后也不会重定向到任何地方

在ReactJS中成功登录后如何重定向到页面?

成功登录后如何重定向到受保护的路由?

成功登录后,我无法重定向到主页

设计:成功登录后重定向到管理路径

登录成功后重定向到UI-Route

重置密码成功后重定向到登录页面