我的数据库表结构式这样的 t_customer: id-username-password 1-admin-$2a$10$YPsBxlg8QeNtLcmEDiH4qO5WAwJLmar/4BKuzmfu3MH/GYTCYM2ae 2-user-$2a$10$0KcJzxj8JYd0EVau1RLud.YbHeINx/xu3fEp.OYeXEPoU6DlqpJOW t_authority id-authority 1-ADMIN 2-USER 根据此数据库写一个自定义认证SecurityConfig

时间: 2023-08-12 22:05:03 浏览: 56
好的,以下是一个示例的自定义认证SecurityConfig: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomUserDetailsService customUserDetailsService; @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/admin/**").hasAuthority("ADMIN") .antMatchers("/user/**").hasAnyAuthority("ADMIN", "USER") .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll(); } @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(customUserDetailsService) .passwordEncoder(passwordEncoder()); } @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` 其中,`CustomUserDetailsService` 是自定义的用户信息服务类,用于查询用户信息和权限信息。我们可以根据 `t_customer` 和 `t_authority` 表来实现这个类,下面是一个示例: ```java @Service public class CustomUserDetailsService implements UserDetailsService { @Autowired private CustomerRepository customerRepository; @Autowired private AuthorityRepository authorityRepository; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { Customer customer = customerRepository.findByUsername(username); if (customer == null) { throw new UsernameNotFoundException("User not found"); } List<Authority> authorities = authorityRepository.findByCustomerId(customer.getId()); List<GrantedAuthority> grantedAuthorities = new ArrayList<>(); for (Authority authority : authorities) { grantedAuthorities.add(new SimpleGrantedAuthority(authority.getAuthority())); } return new User(customer.getUsername(), customer.getPassword(), grantedAuthorities); } } ``` 在这个示例中,我们使用了 `CustomerRepository` 和 `AuthorityRepository` 来实现对 `t_customer` 和 `t_authority` 表的查询操作。根据用户名查询到对应的用户信息后,我们再根据用户 ID 查询该用户的权限信息,并将权限信息转换为 Spring Security 的 `GrantedAuthority` 对象。最后,我们将查询到的用户信息和权限信息封装为一个 `UserDetails` 对象并返回。 最后,我们需要实现 `CustomerRepository` 和 `AuthorityRepository` 接口来实现对 `t_customer` 和 `t_authority` 表的查询操作。这里我们可以使用 Spring Data JPA 来简化操作,以下是一个示例: ```java public interface CustomerRepository extends JpaRepository<Customer, Long> { Customer findByUsername(String username); } public interface AuthorityRepository extends JpaRepository<Authority, Long> { List<Authority> findByCustomerId(Long customerId); } ``` 需要注意的是,`Customer` 和 `Authority` 分别对应 `t_customer` 和 `t_authority` 表的实体类。在实体类中,我们需要使用 `@Table`、`@Id`、`@Column` 等注解来指定表名、主键和字段名。具体实现可以参考以下代码: ```java @Entity @Table(name = "t_customer") public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "username") private String username; @Column(name = "password") private String password; // 省略 getter 和 setter } @Entity @Table(name = "t_authority") public class Authority { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "authority") private String authority; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "customer_id") private Customer customer; // 省略 getter 和 setter } ``` 在 `Authority` 实体类中,我们使用了 `@ManyToOne` 和 `@JoinColumn` 注解来指定与 `Customer` 表的关联关系。这里使用了懒加载策略(`FetchType.LAZY`),即在查询 `Authority` 表时不会立即查询 `Customer` 表,只有在需要时才会查询。

相关推荐

请按如下材料,实现用户登陆接口(提供给前端调用的http接口) 接口名称:登录 请求路径:/customer/open/user/login1 请求方式:post 请求格式:application/json 请求输入: Header:无 payload: userPhone:"手机号码",//字符串 userPassword:"密码" 响应格式:application/json 响应返回:{ "code":200 //业务响应码 "msg":"业务成功" // 结果信息 "data":{ token:登录令牌 //(长整型)}} 数据表以及初始化数据(基于MySQL数据库) -- ---------------------------- -- Table structure for t_user_info -- ---------------------------- DROP TABLE IF EXISTS t_user_info; CREATE TABLE t_user_info ( id bigint(36) NOT NULL COMMENT '用户编号', user_phone varchar(200) NOT NULL COMMENT '手机号', user_password varchar(200) NOT NULL COMMENT '密码', user_location varchar(200) DEFAULT NULL COMMENT '收货地址', user_type tinyint(4) NOT NULL COMMENT '用户类型:1-会员 2-门店管理员 3-平台管理员', create_at datetime DEFAULT NULL COMMENT '创建时间', create_user_id bigint(20) DEFAULT NULL COMMENT '创建人编号', update_at datetime DEFAULT NULL COMMENT '最后更新时间', update_user_id bigint(20) DEFAULT NULL COMMENT '最后更新人编号', user_logo varchar(255) DEFAULT NULL, user_name varchar(255) DEFAULT NULL, PRIMARY KEY (id) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='用户信息表'; -- ---------------------------- -- Records of t_user_info -- ---------------------------- INSERT INTO t_user_info VALUES ('769276897803632640', '13111111111', '123456', null, '3', '2020-10-23 19:11:45', null, null, null, null, null);

最新推荐

recommend-type

数据库实验一 基于Sakila的数据库操作

按要求完成对Sakila数据库中表的增、删、改、查操作,提交实验报告。 数据库 MySQL Sakila 问题1:请一边熟悉 sakila 数据库,一边回答以下问题: 1.sakila.mwb 模型中,表结构里每个字段前面的小标记分别表示什么...
recommend-type

华为OD机试D卷 - 在字符串中找出连续最长的数字串(含“+-”号) - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

输出这段Python代码输出所有3位整数中,个位是5且是3的倍数的整数

``` for i in range(100,1000): if i%10 == 5 and i%3 == 0: print(i) ``` 输出结果: ``` 105 135 165 195 225 255 285 315 345 375 405 435 465 495 525 555 585 615 645 675 705 735 765 795 825 855 885 915 945 975 ```