I'm getting the following error after logging in using the default formlogin(). I'm confused about what I am doing wrong because I keep getting this page when I do it without the in-memory users. I'm using packages too. I'm a bit lost here.
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. There was an unexpected error (type=Not Found, status=404).
This is a Spring Boot w/ Spring Security using the default formlogin() with basic in-memory authentication for two roles: user and admin
I have three packages: 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";
}
}
Can you try updating your controller annotation from @RestController
to @Controller
.
Reason being i can see your controller methods have return type string ie probably an HTML page.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.