繁体   English   中英

Spring 使用 Spring 启动 memory 身份验证白标签状态 404 中的安全表单登录

[英]Spring Boot with Spring Security formlogin in memory auth Whitelabel Status 404

使用默认的 formlogin() 登录后出现以下错误。 我对自己做错了什么感到困惑,因为当我在没有内存用户的情况下执行此操作时,我一直在获取此页面。 我也在使用包。 我在这里有点迷路了。

Whitelabel 错误页面 此应用程序没有针对 /error 的显式映射,因此您将其视为后备。 出现意外错误(类型=未找到,状态=404)。

这是一个 Spring 引导 w/ Spring 安全性使用默认的 formlogin() 和两个角色的基本内存身份验证:用户和管理员

我有三个包:main、config、controller

Package com.demo

 @SpringBootApplication
  public class InMemoryAuthenticationApplication {

  public static void main(String[] args) {
    SpringApplication.run(InMemoryAuthenticationApplication.class, args);
    }   

Package com.demo.configuration

 @Configuration
 @EnableWebSecurity
 public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(final AuthenticationManagerBuilder auth) throws    Exception {
        auth.inMemoryAuthentication()
            .withUser("springuser").password(passwordEncoder().encode("spring123")).roles("USER")
            .and()
            .withUser("springadmin").password(passwordEncoder().encode("admin123"))
            .roles("ADMIN", "USER");

    }


@Override
protected void configure(HttpSecurity http) throws Exception {

    http
    .authorizeRequests()
    .antMatchers("/admin*").hasRole("ADMIN")
    .antMatchers("/user*").hasRole("USER")
    .anyRequest().authenticated()
    .and()
    .formLogin();

    }
@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
    }
}

Package com.demo.controller

@RestController
public class LoginController {

@GetMapping("/admin")
public String userhome () {
    return "admin";
}
@GetMapping("/user")
public String adminhome () {
    return "user";
   }

}  

您能否尝试将 controller 注释从@RestController更新为@Controller

原因是我可以看到您的 controller 方法具有返回类型字符串,即可能是 HTML 页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM