spring - 具有自定义登录表单的 Spring Security 不起作用
问题描述
我有一个带有弹簧安全性的自定义登录表单,登录时我不断收到 404
这是我的网络配置
package coffee.web;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("/regular/index.html");
}
}
这是我的安全配置
package coffee.security;
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;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("peter").password("peter").authorities("ROLE_USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/admin/**").hasRole("USER").antMatchers("/", "/**").permitAll()
.and().formLogin().loginPage("/regular/login.html").permitAll().defaultSuccessUrl("/admin/adminIndex.html").and().csrf().disable();
}
}
这是我的登录 html 表单
<head>
<link href="index.css" type="text/css" rel="stylesheet" />
<link href="adminIndex.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="page">
<div id="bar"></div>
<header>
<h1><img id="logo" src="resources/logo.png" width="300" height="75" /></h1>
</header>
<section id="main">
<div id="container">
<div id="loginDiv">
<form name="login" id="login" action="login" method="POST">
<div id="formDiv">
<label for="addUsername" class="title">Username</label>
<span id="validateUsername" class="error"></span>
<input class="input" type="text" id="addUsername" name="username" />
<br />
<label for="addPassword" class="title">Password</label>
<span id="validatePassword" class="error"></span>
<input class="input" type="text" id="addPassword" name="password" />
<br />
<button id="submit" name="submit" type="submit" class="submit">submit</button>
</div>
</form>
</div>
</div>
</section>
</div>
</body>
即使我关闭了授权,并且只打开了身份验证,登录后我仍然会收到 403 错误代码。即使我仍然可以直接进入 admin.adminIndex.html 并且它可以工作..(授权关闭并且只有身份验证上)。
好的,我用“.and().csrf().disable();”更新了我的安全代码 现在我在登录时收到 404 not found 错误。
解决方案
这也发生在我身上(或者至少是类似的事情)。尝试将.and().csrf().disable()
after添加.defaultSuccessUrl("/admin/adminIndex.html")
到您的配置方法中。
推荐阅读
- azure-sql-database - 使用多个 OrderBy 时应该创建哪个索引
- blockchain - 将 UI 选定字段转换为 Solidity 智能合约
- excel - 如何在将文本格式化为大写时导入文本文件
- javascript - 如何显示两个水平滚动菜单栏
- java - 我可以将用户输入从一个包拉到另一个包吗?
- windows - 在 windows 上安装 ocaml 失败
- calendar - 如何使用 MS Graph API 读取公共日历
- survey - 使用 SurveyMonkey 是否可以为问题提供规范名称?
- mongodb - mongo 索引键限制与索引名称长度
- python - 如何修复 pip:无法导入名称 main?(已安装 pip 并且 pip3 工作正常)