簡體   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