Java 使用自定义表单进行Spring安全登录
我的Spring安全配置:Java 使用自定义表单进行Spring安全登录,java,spring,spring-boot,spring-security,Java,Spring,Spring Boot,Spring Security,我的Spring安全配置: @EnableWebSecurity public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter{ @Autowired private ClientDetailsService clientDetailsService; @Autowired public void globalUserDetails(Auth
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter{
@Autowired
private ClientDetailsService clientDetailsService;
@Autowired
public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("username").password("password")
.authorities("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and().csrf().disable();
}
....
}
我的Spring启动控制器:
@Controller
@RequestMapping("/")
public class IndexController extends BaseController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "login";
}
....
}
您好,我正在尝试将Spring security与定制的登录表单一起使用。
我的问题是:
当我输入localhost:8080/login时,浏览器不会指向我的login.jsp,它只是弹出一个带有文本框的默认表单来输入用户名和密码
在我输入用户名作为用户名,密码作为密码后,它返回身份验证失败
回溯:
Request '/login' matched by universal pattern '/**'
DEBUG - matched
DEBUG - /login at position 1 of 11 in additional filter chain; firing Filter: 'WebAsyncManagerIntegrationFilter'
DEBUG - /login at position 2 of 11 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
DEBUG - /login at position 3 of 11 in additional filter chain; firing Filter: 'HeaderWriterFilter'
DEBUG - /login at position 4 of 11 in additional filter chain; firing Filter: 'LogoutFilter'
DEBUG - Trying to match using Ant [pattern='/logout', GET]
DEBUG - Checking match of request : '/login'; against '/logout'
DEBUG - Trying to match using Ant [pattern='/logout', POST]
DEBUG - Request 'GET /login' doesn't match 'POST /logout
DEBUG - Trying to match using Ant [pattern='/logout', PUT]
DEBUG - Request 'GET /login' doesn't match 'PUT /logout
DEBUG - Trying to match using Ant [pattern='/logout', DELETE]
DEBUG - Request 'GET /login' doesn't match 'DELETE /logout
DEBUG - No matches found
DEBUG - /login at position 5 of 11 in additional filter chain; firing Filter: 'BasicAuthenticationFilter'
DEBUG - Basic Authentication Authorization header found for user 'username'
DEBUG - Authentication attempt using org.springframework.security.authentication.dao.DaoAuthenticationProvider
DEBUG - User 'username' not found
DEBUG - Returning cached instance of singleton bean 'delegatingApplicationListener'
DEBUG - Authentication request for failed: org.springframework.security.authentication.BadCredentialsException: Bad credentials
看起来匿名用户没有访问/登录的权限
http
.authorizeRequests()
.antMatchers("/login").anonymous()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and().csrf().disable();
此外,看起来您正在使用基本身份验证的授权标头对服务器执行请求
调试-找到用户“用户名”的基本身份验证授权标头