单点登录实现准备工作(权限表设计和交互方式选择)
@TOC
手机用户请
横屏
获取最佳阅读体验,REFERENCES
中是本文参考的链接,如需要链接和更多资源,可以关注其他博客发布地址。
平台 | 地址 |
---|---|
CSDN | https://blog.csdn.net/sinat_28690417 |
简书 | https://www.jianshu.com/u/3032cc862300 |
个人博客 | https://yiyuery.github.io/NoteBooks/ |
JPA(Java Persistence API)是Sun官方提出的Java持久化规范。它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据。他的出现主要是为了简化现有的持久化开发工作和整合ORM技术,结束现在Hibernate,TopLink,JDO等ORM框架各自为营的局面。值得注意的是,JPA是在充分吸收了现有Hibernate,TopLink,JDO等ORM框架的基础上发展而来的,具有易于使用,伸缩性强等优点。从目前的开发社区的反应上看,JPA受到了极大的支持和赞扬,其中就包括了Spring与EJB3.0的开发团队。
注意:JPA是一套规范,不是一套产品,那么像Hibernate,TopLink,JDO他们是一套产品,如果说这些产品实现了这个JPA规范,那么我们就可以叫他们为JPA的实现产品。
官网>Spring Data JPA
特性
Spring
和JPA
构建存储库的复杂支持Querydsl
谓词,从而支持类型安全的JPA查询@EnableJpaRepositories
实现基于JavaConfig
的存储库配置Spring Data JPA - 参考文档
Spring Data存储库抽象中的中央接口是Repository
。它将域类以及域类的ID类型作为类型参数进行管理。此接口主要用作标记接口,用于捕获要使用的类型,并帮助您发现扩展此接口的接口。该CrudRepository
规定对于正在管理的实体类复杂的CRUD功能。
定义接口
/*
* @ProjectName: 编程学习
* @Copyright: 2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.
* @address: http://xiazhaoyang.tech
* @date: 2019/4/27 20:33
* @email: xiazhaoyang@live.com
* @description: 本内容仅限于编程技术学习使用,转发请注明出处.
*/package com.example.security.dao.common.mapper;import com.example.security.dao.common.model.UserInfo;import org.springframework.dao.IncorrectResultSizeDataAccessException;import org.springframework.data.domain.Example;import org.springframework.data.domain.Page;import org.springframework.data.domain.Pageable;import org.springframework.data.domain.Sort;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.repository.CrudRepository;import javax.persistence.EntityManager;import javax.persistence.EntityNotFoundException;import java.util.List;import java.util.Optional;/**
* <p>
*
* </p>
*
* @author xiazhaoyang
* @version v1.0.0
* @date 2019/4/27 20:33
* @modificationHistory=========================逻辑或功能性重大变更记录
* @modify By: {修改人} 2019/4/27
* @modify reason: {方法名}:{原因}
* ...
*/public interface UserInfoRepository extends JpaRepository<UserInfo, Long> {
}
定义数据库关联DTO
package com.example.security.dao.common.model;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;import java.util.Date;import lombok.AllArgsConstructor;import lombok.Builder;import lombok.Data;import lombok.NoArgsConstructor;import javax.persistence.*;/**
* This class was generated by Capsule Code Generator.
* This class corresponds to the database table tb_common_user_info
*/@Data@Builder@NoArgsConstructor@AllArgsConstructor@ApiModel(value="UserInfo",description="数据库表:tb_common_user_info")@Entity@Table(name="tb_common_user_info")public class UserInfo { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @ApiModelProperty(value="人员ID",name="userId",example="2",dataType="long", required=true) private Long userId; @ApiModelProperty(value="年龄",name="age",example="1",dataType="int", required=true) private Integer age; @ApiModelProperty(value="姓名",name="userName",example="userName_60NQweMhSS",dataType="string", required=true) private String userName; @ApiModelProperty(value="密码",name="password",example="password_sROp1uWGG2",dataType="string") private String password; @ApiModelProperty(value="邮箱",name="email",example="email_VImGCTK5ZO",dataType="string", required=true) private String email; @ApiModelProperty(value="备注",name="remark",example="remark_XBaGoG7WC3",dataType="string") private String remark; @ApiModelProperty(value="表明数据是否已删除 0-未删除,1-已删除",name="isDelete",example="2",dataType="int") private Integer isDelete; @ApiModelProperty(value="创建时间",name="createTime",example="2019-04-27 20:11:03",dataType="date", required=true) private Date createTime; @ApiModelProperty(value="权限组",name="roleGroupId",example="1",dataType="int") private Integer roleGroupId;
}
配置扫描和数据库表自动生成
/*
* @ProjectName: 编程学习
* @Copyright: 2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.
* @address: http://xiazhaoyang.tech
* @date: 2019/4/27 20:35
* @email: xiazhaoyang@live.com
* @description: 本内容仅限于编程技术学习使用,转发请注明出处.
*/package com.example.security.config;import org.springframework.context.annotation.Configuration;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;/**
* <p>
*
* </p>
*
* @author xiazhaoyang
* @version v1.0.0
* @date 2019/4/27 20:35
* @modificationHistory=========================逻辑或功能性重大变更记录
* @modify By: {修改人} 2019/4/27
* @modify reason: {方法名}:{原因}
* ...
*/@EnableJpaRepositories(basePackages = "com.example.security.dao.common")@Configurationpublic class DataBaseConfig {
}
spring:
datasource:
username: root
password: 12345
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xx.xx.203.181:3306/db_capsule?useUnicode=true&characterEncoding=utf-8
jpa:
hibernate:
ddl-auto: update
show-sql: true
注入使用
package com.example.security.service;import com.example.security.dao.common.model.UserInfo;import org.springframework.security.core.userdetails.UserDetailsService;/**
* <p>
*
* </p>
*
* @author xiazhaoyang
* @version v1.0.0
* @date 2019/4/24 07:47
* @modificationHistory=========================逻辑或功能性重大变更记录
* @modify By: {修改人} 2019/4/24
* @modify reason: {方法名}:{原因}
* ...
*/public interface CapUserDetailsService extends UserDetailsService { /**
* 注册
* @param userInfo
*/
void register(UserInfo userInfo);
}
/*
* @ProjectName: 编程学习
* @Copyright: 2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.
* @address: http://xiazhaoyang.tech
* @date: 2019/4/24 07:48
* @email: xiazhaoyang@live.com
* @description: 本内容仅限于编程技术学习使用,转发请注明出处.
*/package com.example.security.service.impl;import com.example.security.dao.common.mapper.UserInfoRepository;import com.example.security.dao.common.model.UserInfo;import com.example.security.service.CapUserDetailsService;import org.springframework.security.core.userdetails.UserDetails;import org.springframework.security.core.userdetails.UsernameNotFoundException;import org.springframework.security.crypto.password.PasswordEncoder;import org.springframework.stereotype.Service;import javax.annotation.Resource;import java.util.Date;/**
* <p>
*
* </p>
*
* @author xiazhaoyang
* @version v1.0.0
* @date 2019/4/24 07:48
* @modificationHistory=========================逻辑或功能性重大变更记录
* @modify By: {修改人} 2019/4/24
* @modify reason: {方法名}:{原因}
* ...
*/@Service("capUserDetailsService")public class CapUserDetailsServiceImpl implements CapUserDetailsService { @Resource
private UserInfoRepository userInfoRepository; @Resource
private PasswordEncoder passwordEncoder; /**
* Locates the user based on the username. In the actual implementation, the search
* may possibly be case sensitive, or case insensitive depending on how the
* implementation instance is configured. In this case, the <code>UserDetails</code>
* object that comes back may have a username that is of a different case than what
* was actually requested..
*
* @param username the username identifying the user whose data is required.
* @return a fully populated user record (never <code>null</code>)
* @throws UsernameNotFoundException if the user could not be found or the user has no
* GrantedAuthority
*/
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { return null;
} @Override
public void register(UserInfo userInfo) { //加密
userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
userInfo.setCreateTime(new Date());
userInfoRepository.save(userInfo);
}
}
查看结果
请求脚本触发
数据库