登录后弹簧安全与弹簧启动无法重定向

时间:2018-05-15 03:48:03

标签: java spring spring-boot spring-security

在将Spring安全性与spring boot集成时遇到了问题。覆盖WebSecurityConfigurerAdapter后,当我访问登录页面时,我无法重定向到成功页面(wlecome.ftl)。它将重定向到登录页面(index.ftl)没有错误日志。

我有没有错过?感谢您的帮助,谢谢!

  

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(登录页面)

<!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>

2 个答案:

答案 0 :(得分:0)

您没有设置成功的登录网址,并且您在登录成功后可以在任何位置重定向。把:

defaultSuccessUrl("/user/welcome")

之后:

passwordParameter("...")

在您的安全配置中。

答案 1 :(得分:0)

尝试

W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection