[英]Spring security in angularjs returns 403 forbidden for login
[英]Custom formLogin() in Spring security returns a (type=Forbidden, status=403)
我已经使用 Spring Security 和 Spring Web 的依赖项设置了一个 Spring 启动应用程序。 我在这个例子中使用了 inMemoryAuthentication()。 我设置了 3 个 html 页面并将它们放在静态文件夹中(我没有使用 Thymeleaf 或 JSP 页面,只是纯 html)。
当我使用默认的 formLogin() 并运行应用程序时,我得到了 spring security 的默认登录页面,一旦我输入用户和密码,我就可以按预期获得目标页面 dash.html。
当我使用自定义的 formLogin() ,运行应用程序时,我得到状态 403 类型禁止:
白标错误页面
此应用程序没有明确的 /error 映射,因此您将其视为后备。
12 月 12 日星期四 10:10:14 IST 2019 出现意外错误(类型=禁止,状态=403)。
禁止的
我在 StackOverflow 中搜索,也在下面的链接中搜索,但没有看到任何解决方案(在链接中它使用 Thymeleaf ,而我使用的是放在 resources/static 文件夹中的 HTML 页面)
有人也有这个问题吗?
请指教,
问候, 沙勒姆
相关数据和代码:
我设置 Spring Security 代码如下:
package com.rc1.conig;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("shalem")
.password(passwordEncoder().encode("12"))
.roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/dash/**").authenticated()
.and()
.formLogin()
.loginPage("/mylogin")
.permitAll();
}
}
- 控制器代码:
package com.rc1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DashController {
@RequestMapping("/dash")
public String getDashboard() {
return "dash.html";
}
}
package com.rc1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginController {
@RequestMapping("/mylogin")
public String getLogin() {
return "login-page.html";
}
}
* HTML页面:
<!-- index.html page -->
<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1255">
<title>Home</title>
</head>
<body>
<h1>Home Page</h1>
<h3>
<a href="http://localhost:8080/dash">dashboard</a>
</h3>
</body>
</html>
<!-- Customized login-page.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1255">
<title>mylogin</title>
</head>
<body>
<form action="http://localhost:8080/mylogin" method="post">
<p>
user: <input type="text" name="user">
</p>
<p>
pass :<input type="password" name="password">
</p>
<button type="submit">login</button>
</form>
</body>
</html>
<!-- dash.html page -->
<!DOCTYPE html>
<html>
<head>
<meta charset="windows-1255">
<title>Insert title here</title>
</head>
<body>
<h1>dashboard receieved</h1>
</body>
</html>
如果要使用自定义登录页面,则应指定用户名和密码提交到的位置。 所以,只要改变这里,
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/dash/**").authenticated()
.and()
.formLogin()
.loginPage("/mylogin")
.loginProcessingUrl("/perform_login")
.defaultSuccessUrl("/homepage")
.permitAll();
}
之后,任何匿名用户点击任何经过身份验证的 url,然后重定向到
/mylogin
在该用户输入用户名和密码之后,该用户名和密码提交给
/perform_login
如果凭据有效,则重定向到
/主页
否则回到
/mylogin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.