登录后无法重定向spring boot的spring security [英] spring security with spring boot cant redirect after login

查看:67
本文介绍了登录后无法重定向spring boot的spring security的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在spring boot中集成spring security时遇到问题.覆盖WebSecurityConfigurerAdapter后,访问登录页面时无法重定向到成功页面(wlecome.ftl).(index.ftl) 没有错误日志.

有什么我错过的吗?感谢帮助,谢谢!

<块引用>

SecurityConfig.java

@Configuration@启用网络安全@EnableGlobalMethodSecurity(prePostEnabled = true)公共类 SecurityConfig 扩展了 WebSecurityConfigurerAdapter {@自动连线MyUserDetailsS​​ervice detailsS​​ervice;@覆盖protected void configure(HttpSecurity http) 抛出异常 {http.authorizeRequests().antMatchers("/user/**").hasRole("USER").anyRequest().fullyAuthenticated().and().formLogin().loginPage("/登录").failureUrl("/login?error=true").usernameParameter("用户名").passwordParameter("密码").defaultSuccessUrl("/user/welcome")//添加这个但是不行.permitAll().and().注销().logoutUrl("/注销").permitAll();}@覆盖公共无效配置(WebSecurity web)抛出异常{web.ignoring().antMatchers("/js/**", "/css/**", "/images/**", "/**/favicon.ico");}@覆盖公共无效配置(AuthenticationManagerBuilder auth)抛出异常{auth.inMemoryAuthentication().withUser("admin").password("admin").roles("USER");}

<块引用>

登录.控制器

@Controller公共类登录控制器{@RequestMapping("/登录")公共字符串登录(){返回索引";}@RequestMapping("/用户/欢迎")公共字符串欢迎(){返回用户/欢迎";}

<块引用>

index.ftl(登录页面)

<#import "/spring.ftl" 作为 spring/><html lang="zh-cn"><头><meta charset="UTF-8"><title>标题</title><身体><form action="<@spring.url '/login'/>"方法=发布">用户名:<input type="text" name="username"><br>密码:
<输入类型=提交"值=登录"></表单></html>

<块引用>

欢迎.ftl

<头><身体>欢迎</html>

解决方案

试试这个

.defaultSuccessUrl("/user/welcome",true)

I encountered a problem when integrating spring security with spring boot.After Overwriting the WebSecurityConfigurerAdapter, I can't redirect to the successful page(wlecome.ftl) when I've accessed the login page.It's aways redirect to the login page(index.ftl) without error logs.

Is there anyting I missed?Help is aways appreciated,thanks!

SecurityConfig.java

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    MyUserDetailsService detailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/user/**").hasRole("USER")
                .anyRequest().fullyAuthenticated()
                .and()
                .formLogin()
                .loginPage("/login")
                .failureUrl("/login?error=true")
                .usernameParameter("username")
                .passwordParameter("password")
                .defaultSuccessUrl("/user/welcome")//add this but not work
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
                .permitAll();
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/js/**", "/css/**", "/images/**", "/**/favicon.ico");
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("USER");
    }

Login.Controller

@Controller
public class LoginController {

    @RequestMapping("/login")
    public String login(){
        return "index";
    }

    @RequestMapping("/user/welcome")
    public String welcome(){
        return "user/welcome";
    }

index.ftl(the login page)

<!DOCTYPE html>
<#import "/spring.ftl" as spring />
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="<@spring.url '/login' />" method="post">
    username:<input type="text" name="username"><br>
    password:<input type="password" name="password"><br>
    <input type="submit" value="login">
</form>

</body>
</html>

welcome.ftl

<html>
  <head>

  </head>
<body>
  welcome
</body>
</html>

解决方案

try this

.defaultSuccessUrl("/user/welcome",true)

这篇关于登录后无法重定向spring boot的spring security的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆