当前位置: 首页 > news >正文

Spring Security自定义登陆界面和密码验证逻辑

maven依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>

创建配置文件处理跳转拦截等功能以及密码比对功能

package com.example.demo2.demos.web1;import org.springframework.beans.factory.annotation.Autowired;
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.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;@Configuration
@EnableWebSecurity
public class WebConfig extends WebSecurityConfigurerAdapter {@AutowiredUserDateService userDateService;@Overrideprotected void configure(HttpSecurity http) throws Exception {/***  1. 允许访问路径"/"或者"/home",而不需要登录。*  除过"/"或者"/home",其他页面都需要登录。*  2. 表单登录,路径是"/login",允许所有用户都可以发起登录操作*  3. 允许所有用户都可以退出登录*/http.formLogin().loginPage("/login.html")    //登录页面设置//配置从前端传递过来的用户名和密码字段名//.usernameParameter("name")//.passwordParameter("password").loginProcessingUrl("/user/login")   //点击登录后访问的路径.defaultSuccessUrl("/index.html").permitAll()   //登录成功后跳转路        径,permitAll表示无条件进行访问.and().authorizeRequests().mvcMatchers("/","/user/login").permitAll() //设置不需要认证可以直接访问的 路径.anyRequest().authenticated()//关闭csrf.and().csrf().disable().logout().logoutUrl("/logout").logoutSuccessUrl("/login?logout").permitAll();}//密码加密@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}//密码比对@Autowiredpublic void configure(AuthenticationManagerBuilder auth) throws Exception {auth.userDetailsService(userDateService);}
}

创建userservice

package com.example.demo2.demos.web1;import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;import java.util.ArrayList;/*** @author FAN BOY* @version 1.0* @date 2020/7/20 14:22*/
@Service
public class UserDateService implements UserDetailsService {@Overridepublic UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();//这是通过username查询获取一组数据将获得的数据传给security的User//sql语句自己写com.example.demo2.demos.web2.User user =new com.example.demo2.demos.web2.User("admin",passwordEncoder.encode("123"),12);//这里我们使用静态类来模拟通过数据库获取的user类
//        if (user == null) {
//            throw new UsernameNotFoundException("该用户不存在!");
//        }//把获得的账号密码传给security的User//注意此时的User是security自带的的User类,第一个参数为用户名,第
二个为密码,一般来说我们从数据库获取的为加密后的密码,所以我们通过上面配置
文件的密码加密自己进行加密,第三个为权限角色,我没有做处理return new User(user.getUsername(),user.getPassword(),new ArrayList<>());}
}

自定义登陆界面login.html

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>登录页面</title>
</head>
<body>
<form action="/user/login" method="post">
//注意用户名和密码 为username和password,security默认为这
两个字段,如果你要用其他的名称,需要在第一个配置文件中的
loginForm中进行配置用户名:<input type="text" name="username"/><br>密码:<input type="password" name="password"/><br><input type="submit" value="登录"/>
</form>
</body>
</html>

上面的UserDateService 中我们固定返回一个用户名密码为admin        123的user类

然后security的configure(AuthenticationManagerBuilder auth)进行密码对比,注意这里只是比较密码,因为你既然返回user类了就说明你的username是存在的,(小问题,一般来说不会有问题因为我是固定返回的一个类,我在前端只要是密码输入正确就能登陆~)

上面就实现了一个自定义登陆界面以及密码验证的基本功能的(由于公司电脑不让本地装数据库,就用静态类代替了,如果要链接数据库也就是添加一个查询数据库方法而已)。

http://www.lryc.cn/news/124377.html

相关文章:

  • Android布局【LinearLayout】
  • 搭建grafana+loki+promtail日志收集系统
  • Electron+vue3项目使用SQLite3数据库
  • SpringSpringBoot常用注解
  • 题目:2566.替换一个数字后的最大差值
  • 使用 NLP 进行文本摘要
  • vue3挂载全局方法和组件
  • mybatisplus学习笔记
  • go mod 添加私有库GOPRIVATE
  • 07-HDFS入门及shell命令
  • TiDB在科捷物流神州金库核心系统的应用与实践
  • React 18 更新 state 中的数组
  • 【css】css中使用变量var
  • 判断自己网络所在的NAT类型
  • ClickHouse(十九):Clickhouse SQL DDL操作-1
  • 小程序保留2位小数据,不四舍五入
  • 【linux-nginx】nginx限流以及控制访问方法
  • 菜单和内容滚动的联动原理及代码
  • Python爬虫:单线程、多线程、多进程
  • 超强的Everything,吊打系统自带文件搜索功能!
  • flink配置参数
  • 学习Vue:安装Vue.js和设置开发环境
  • 代理技术在网络安全、爬虫和数据隐私中的多重应用
  • Python 3 使用Hadoop 3之MapReduce总结
  • KU Leuven TU Berlin 推出“RobBERT”,一款荷兰索塔 BERT
  • Postern中配置和使用Socks5代理指南
  • android 窗口级模糊实现方式
  • 面试热题(数组中的第K个最大元素)
  • HTTP2协议介绍
  • 矩阵的转置