我正在尝试为Spring Boot
和ReactJS
的Spring Security
设置自定义登录页面。我现在决定在没有ReactJS
的情况下工作,只是使用Thymeleaf模板作为登录页面。
如果我没有在我的Spring
SecurityConfiguration
中提供loginPage()
到formLogin()
,我可以通过默认登录屏幕登录,身份验证在应用程序中工作。
当我尝试在SecurityConfiguration
内设置自定义登录页面时,问题出现了,当我进入应用程序时,我无法让它正确加载登录。
.formLogin().loginPage("/login")
当我转到8081端口上的默认地址(通过application.properties配置):
http://localhost:8081/
我会被送到/templates/index.html
而不是login.html
当我view source
?
Spring Security
的登录要求来测试这一点<form th:action="@{/login}" method="post">
<div><label> User Name : <input type="text" name="username"/> </label></div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Sign In"/></div>
</form>
这将加载字段登录,但我不想让它加载索引,我想让它加载登录。我的问题是,为什么它把我发送到index.html,而不是login.html?
- 我在设置中错过了什么,允许去login.html而不是index.html?
是否与permitAll()匹配器的设置有关?
.authorizeRequests()
.antMatchers("/", "/built/**", "/main.css").permitAll()
谢谢下面
代码
春SecurityConfiguration '
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private SpringDataJpaUserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(this.userDetailsService)
.passwordEncoder(PasswordEncoder.PASSWORD_ENCODER);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/built/**", "/main.css").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/#/login") // for reactjs
//.loginPage("/login") // for login.html
.permitAll()
.defaultSuccessUrl("/", true)
.and()
.httpBasic()
.and()
.csrf().disable() // TODO enable for production
.logout()
.invalidateHttpSession(true)
.deleteCookies("JSESSIONID")
.logoutSuccessUrl("/");
}
}
index . html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>M Cellblock</title>
<!-- cerulean style -->
<link rel="stylesheet" href="https://bootswatch.com/cerulean/bootstrap.css" />
<link rel="stylesheet" href="https://bootswatch.com/cerulean/bootstrap.min.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css" />
<link rel="stylesheet" href="/main.css" />
<link rel="stylesheet" href="/react-date-picker.css" />
</head>
<body>
<div id="react"></div>
<script src="built/bundle.js"></script>
</body>
</html>
login.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Spring Security Example </title>
</head>
<body>
<div th:if="${param.error}">
Invalid username and password.
</div>
<div th:if="${param.logout}">
You have been logged out.
</div>
<form th:action="@{/login}" method="post">
<div><label> User Name : <input type="text" name="username"/> </label></div>
<div><label> Password: <input type="password" name="password"/> </label></div>
<div><input type="submit" value="Sign In"/></div>
</form>
</body>
</html>
第二次尝试
antMatchers("")
规则是按照给定的顺序应用的,所以如果你首先允许访问"/"
到all
的身份验证是不需要的,可能这就是index
页面被加载的原因。尝试从permitAll
列表中删除"/"
。请求任何其他的东西都需要身份验证并重定向到登录页面。
在我发布的配置中就是这样做的。
第一次尝试
在我的vadin应用程序中添加:
.exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login/"))
到HttpSecurity配置完成工作(但我也有servlet映射到/login
)。
这是我的完整配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/vaadinServlet/**", "/VAADIN/**", "/PUSH/**",
"/UIDL/**", "/login", "/login/**", "/register",
"/environment").permitAll()
.antMatchers("/**").fullyAuthenticated()
.and()
.csrf().disable()
.exceptionHandling().authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/login/"))
.and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
}