java - 春季用jsp记录页面
问题描述
我得到了这个用于记录页面的 jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>Login Page</title>
<style>
.error {
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
color: #a94442;
background-color: #f2dede;
border-color: #ebccd1;
}
.msg {
padding: 15px;
margin-bottom: 20px;
border: 1px solid transparent;
border-radius: 4px;
color: #31708f;
background-color: #d9edf7;
border-color: #bce8f1;
}
#login-box {
width: 300px;
padding: 20px;
margin: 100px auto;
background: #fff;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border: 1px solid #000;
}
</style>
</head>
<body onload='document.loginForm.username.focus();'>
<h1>Spring Security Custom Login Form (Annotation)</h1>
<div id="login-box">
<h2>Login with Username and Password</h2>
<c:if test="${not empty error}">
<div class="error">${error}</div>
</c:if>
<c:if test="${not empty msg}">
<div class="msg">${msg}</div>
</c:if>
<form name='loginForm'
action="<c:url value='j_spring_security_check' />" method='POST'>
<table>
<tr>
<td>User:</td>
<td><input type='text' name='user' value=''></td>
</tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password' /></td>
</tr>
<tr>
<td colspan='2'>
<input name="submit" type="submit" value="submit" />
</td>
</tr>
</table>
<input type="hidden"
name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
</div>
</body>
</html>
在控制器中:
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView login(
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject("error", "Invalid username and password!");
}
if (logout != null) {
model.addObject("msg", "You've been logged out successfully.");
}
model.setViewName("login");
return model;
}
和安全配置:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("admin").password("{noop}123456").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/admin/**").access("hasRole('ROLE_USER')").and().formLogin()
.loginPage("/login").permitAll().failureUrl("/login?error").usernameParameter("username")
.passwordParameter("password").and().logout().logoutSuccessUrl("/login?logout").and().csrf();
}
}
登录后,我没有得到有效的网址,例如:localhost/com.myproject.spring/login?error 或 localhost/com.myproject.spring/admin
但我总是得到:
localhost/com.myproject.spring/j_spring_security_check
和错误消息: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
出了什么问题?我刚刚从本教程中复制了所有内容:https ://www.mkyong.com/spring-security/spring-security-custom-login-form-annotation-example/
解决方案
推荐阅读
- php - 从数组 Laravel 中获取元素总和
- html - 将数据发送到 Discord webhook 的 HTML 表单
- javascript - 继续一个无限的 CSS 动画,直到最后一帧才停止
- java - Android Gradle 插件导致的重复类
- c# - 如何在 while 循环中使用“char”作为标记值
- apache-kafka - 将文件上传到 Kafka 并进一步处理?
- ruby-on-rails - 通过 rails 控制台添加用户的问题
- angular - Angular 接口中的 atob
- c++ - 为什么灯光出现在对面?
- javascript - TestCafe visibilityCheck 不等待元素出现