html - Spring Security 自定义登录页面不允许我进入
问题描述
通过自定义登录页面登录应用程序时遇到问题。应用程序控制台中没有错误,但是当我单击登录时,URL从http://localhost:8080/login更改为http://localhost:8080/login?error。我找不到原因,我会将我的课程和视图放在下面,非常感谢您的帮助。
安全配置类
package myProject.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.StandardPasswordEncoder;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@SuppressWarnings("deprecation")
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userService;
@Override
public void configure(HttpSecurity theHttp) throws Exception {
theHttp.authorizeRequests()
.antMatchers("/design", "/order")
.access("hasRole('ROLE_USER')")
.antMatchers("/", "/**")
.access("permitAll")
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/", true)
.and()
.logout()
.logoutSuccessUrl("/login")
.and()
.csrf()
.ignoringAntMatchers("/h2-console/**")
.and()
.headers()
.frameOptions()
.sameOrigin();
}
@Bean
public PasswordEncoder encoder() {
return new StandardPasswordEncoder("53cr3t");
}
@Override
public void configure(AuthenticationManagerBuilder theAuth) throws Exception {
theAuth.userDetailsService(userService)
.passwordEncoder(encoder()/*encoder*/);
}
}
登录视图
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>My Project</title>
</head>
<body>
<h1>Login</h1>
<img th:src="@{/images/image.png}">
<div th:if="${error}">
Unable to login. Check your username and password.
</div>
<p>New here? Click
<a th:href="@{/register}"> to rgister.</a>
</p>
<form method="POST" th:action="@{/login}" id="loginForm">
<!-- User name -->
<label for="username">Username: </label>
<input type="text" name="username" id="username" /> <br/>
<!-- Password -->
<label for="password">Password: </label>
<input type="password" name="password" id="password" /> <br/>
<!-- Login button -->
<input type="submit" value="Login"/>
</form>
</body>
</html>
这条线在consloe: -2021-03-21 09:41:48.380 INFO 9276 --- [nio-8080-exec-3] c.vaadin.flow.spring.SpringInstantiator : The number of beans implementing 'I18NProvider' is 0. Cannot use Spring beans for I18N, falling back to the default behavior
解决方案
错在UserRepositoryDetailsService
课堂上。它被注释为@Service
但在我的代码@Service
中由import org.jvnet.hk2.annotations.Service;
. 但是它应该从import org.springframework.stereotype.Service;
. 一旦我解决了它,所有其他问题都解决了。
推荐阅读
- python - downloading and installing a program with the code
- r - 从图中删除多余的灰色垂直线
- amazon-web-services - events.amazonaws.com 不能担任角色
- node.js - nodejs 中的无服务器 Excel 导出
- linux - 根据 file 命令确定如何解压存档
- python - dateutil 与 django 冲突
- javascript - 在javascript中将字符串转换为多维数组
- dataset - 为什么我的 dataset.data 的每个图像的形状都不同?
- c# - 如何通过调用ajax post保存数据时显示模型状态错误
- javascript - 如何从chrome中的页面中删除具有某个类的所有元素