gpt4 book ai didi

spring - 使用 Spring Boot 进行内存身份验证

转载 作者:行者123 更新时间:2023-12-02 01:35:08 25 4
gpt4 key购买 nike

我使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎生成了一个 Spring Boot Web 应用程序,并将其打包为可执行 JAR 文件。

使用的技术:

Spring Boot 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat Embed 8.5.6、Maven 3、Java 8

这是我的安全配置类:

@Configuration
@EnableWebSecurity
@PropertySource("classpath:/com/tdk/iot/config/app-${APP-KEY}.properties")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Value("${securityConfig.formLogin.loginPage}")
private String loginPage;

@Override
protected void configure(HttpSecurity http) throws Exception {

http
.formLogin()
.loginPage(loginPage)
.permitAll()
.loginProcessingUrl("/login")
.failureUrl("/login.html?error=true")
.defaultSuccessUrl("/books/list")
.and()
.exceptionHandling()
.accessDeniedPage("/denied")
.and()
.authorizeRequests()
.antMatchers("/mockup/**").permitAll()
.antMatchers("/books/**").permitAll()
.antMatchers("/welcome/**").authenticated()
.and()
.logout()
.permitAll()
.logoutSuccessUrl("/index.html");
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.passwordEncoder(new StandardPasswordEncoder())
.withUser("test1").password("test1").roles("ADMIN").and()
.withUser("test2").password("test2").roles("USER").and()
.withUser("test3").password("test3").roles("SUPERADMIN");
}

@Bean
public static PropertySourcesPlaceholderConfigurer propertyDefaultConfig() {
return new PropertySourcesPlaceholderConfigurer();
}
}

这里是登录 Controller

 @Controller
public class LoginController {

@RequestMapping(value={ "/", "/tdk/login"}, method = { RequestMethod.POST,RequestMethod.GET})
public String welcome(Map<String, Object> model) {
return "tdk/login";
}
}

和模板:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>

<div class="wrap">
<div class="login">
<div class="logo"></div>

<form th:action="@{/login.html}" method="post">

<p th:if="${loginError}" class="error">Wrong user or password</p>

<div class="input_label"><i class="fa fa-user"></i><input type="text" name="user" placeholder="User" /></div>
<div class="input_label"><i class="fa fa-key"></i><input type="password" name="pass" placeholder="Password" /></div>
<input type="submit" value="LOGIN" />
</form>
<div class="forget">
<!-- <a href="#">Do you forgot your password?</a><br/> -->
<br/>
</div>
</div>
</div>

</body>
</html>

但是当我使用 test1/test1 访问时,出现此错误:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Mar 05 20:16:11 CET 2017 There was an unexpected error (type=Method Not Allowed, status=405). Request method 'POST' not supported

最佳答案

您的登录页面使用 HTTP POST 调用 /login.html,但您的服务器不提供此类请求映射。

Spring Security 配置中配置的 URL:

.loginProcessingUrl("/login")

与您的登录页面中的 URL 不匹配:

<form th:action="@{/login.html}" method="post">

另请参阅AbstractAuthenticationFilterConfigurer#loginProcessingUrl :

Specifies the URL to validate the credentials.

关于spring - 使用 Spring Boot 进行内存身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42613899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com