Spring引导安全自定义登录页面
我想知道我应该如何创建自己的自定义登录页面?我尝试了以下指南,例如,但没有效果,因为我似乎无法让Spring引导安全自定义登录页面,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,我想知道我应该如何创建自己的自定义登录页面?我尝试了以下指南,例如,但没有效果,因为我似乎无法让WebMvcConfig类正常工作。我一直收到404个错误。这是我目前的档案 安全配置类 @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter{ /** * */ @Override protected void
WebMvcConfig
类正常工作。我一直收到404个错误。这是我目前的档案
安全配置类
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{
/**
*
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and()
.formLogin().loginPage("/login").permitAll();
http.authorizeRequests().antMatchers("/stats").hasRole("ADMIN")
.and().formLogin().loginPage("/login");
//everything else ADMIN and USERS can see
http.authorizeRequests().antMatchers("/").hasAnyRole("ADMIN","USER")
.and().formLogin().loginPage("/login");
}
@Autowired
DataSource dataSource;
@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.passwordEncoder(passwordEncoder());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
login.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>HERE!!!!</h1>
<form action="${contextRoot}/login" method="post">
<input type="text" name="username" value="username"/>
<input type="password" name="password"/>
<input type="submit" value="Login"/>
</form>
</body>
</html>
您可以尝试以下方法:
下面的示例在JSF和Spring中
不走运,我想我真正的问题是登录页面是否需要显式控制器?不需要!!为什么您的页面将直接向spring注册?您可以在以下网站上找到更多信息: