学习视频教程Spring Security开发REST服务到 4-4 自定义用户认证逻辑--自定义登录页

一。 自定义登录界面,需要先创建一个自己的登录界面(存放位置:在resources中建立目录resources,在这个目录中新建html文件用来登录),并在securtyConfig类中进行说明:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>我的登录页-demo</title>
</head>
<body>
	<h2>标准登录信息</h2>
	<h3>表单登录</h3>
	<form action="/login" method = "post">
		<table>
			<tr>
				<td>用户名:</td>
				<td><input type = "text" name ="username"></td>
			</tr>
			<tr>
				<td>密码:</td>
				<td><input type = "password" name ="password"></td>
			</tr>
			<tr>
				<td colspan="2"><button type="submit">登录</button></td>
			</tr>
			
		</table>
	</form>
</body>
</html>
@EnableWebSecurity
public class SecurtyConfig extends WebSecurityConfigurerAdapter {
	
	@Bean
	public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
	
    /**
     * 自定义配置
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin() //基于Form表单登录验证
        	.loginPage("/mylogin.html")  //自定义登录页
            .loginProcessingUrl("/login")  //表单提交相匹配,否则无法登录
        	.and()
        	.authorizeRequests()
        	.antMatchers("/mylogin.html").permitAll()   //防止登录页也需要进行登录验证,添加这个说明登录页不进行验证
        	.anyRequest()
        	.authenticated();
        	
    }  

添加后,发现还是不能正常运行,是因为spring security自动包含跨站伪造防护功能,我们需要先进行关闭;

 protected void configure(HttpSecurity http) throws Exception {
        http.formLogin() //基于Form表单登录验证
        	.loginPage("/mylogin.html")  //自定义登录页
        	.loginProcessingUrl("/login")  //表单提交相匹配,否则无法登录
        	.and()
        	.authorizeRequests()
        	.antMatchers("/mylogin.html").permitAll()   //防止登录页也需要进行登录验证,添加这个说明登录页不进行验证
        	.anyRequest()
        	.authenticated()
        	.and()
        	.csrf().disable();	//	关闭spring security的跨站伪造防护功能
    } 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值