我想知道如何创建自己的自定义登录页面?我尝试遵循https://docs.spring.io/spring-security/site/docs/current/guides/html5/form-javaconfig.html和http://www.baeldung.com/spring-mvc-tutorial#configviews之类的指南无济于事,因为我似乎无法使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();
}
}
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>
和Web Mvc配置类
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
答案 0 :(得分:0)
您可以尝试以下操作: 以下示例在JSF和Spring中 https://www.codenotfound.com/jsf-primefaces-spring-security-example.html