适用基于springboot或spring创建的项目,依赖spring-data-jpa、shiro,与springboot集成较方便
git地址: https://gitee.com/UnlimitedBladeWorks_123/spring-biz-module/tree/master/win-security
compile("com.winbaoxian.module:win-security:1.0.0-SNAPSHOT")
使用jar包中security.sql创建相关表,用户、角色、资源及关系表
@EnableWinSecurity(entityManagerFactoryRef = "entityManagerFactoryTob",
transactionManagerRef = "transactionManagerTob")
说明
WinSecurityAccessService.login(String userName);
WinSecurityAccessService.logout();
http://doc.suiyiwen.com/win-security/
可分别控制用户管理、资源管理、角色管理、登录用户数据接口,默认全部生效
@EnableWinSecurity设置extensionUserDTO、extensionUserEntity
extensionUserDTO 用户前端请求对象
@Data
public class BrokerageAdminUserDTO extends WinSecurityBaseUserDTO {
@JsonIgnore
private String password;
private String token;
private Long topDepartmentId;
private Long subDepartmentId;
private String position;
private String logoImg;
private Integer sex;
private Date entryTime;
private String idCard;
private String cityName;
private Long cityId;
private String storeCode;
private String ossUserName;
private String remark;
private Integer type;
private Integer serviceCount;
private Integer bindingCount;
private Boolean isPerson;
private Boolean isCar;
}
@Entity
@DynamicInsert
@DynamicUpdate
@Data
public class BrokerageAdminUserEntity extends WinSecurityBaseUserEntity {
@Column(name = "password")
private String password;
@Column(name = "token")
private String token;
@Column(name = "top_department_id")
private Long topDepartmentId;
@Column(name = "sub_department_id")
private Long subDepartmentId;
@Column(name = "position")
private String position;
@Column(name = "logo_img")
private String logoImg;
@Column(name = "sex")
private Integer sex;
@Column(name = "entry_time")
private Date entryTime;
@Column(name = "id_card")
private String idCard;
@Column(name = "city_name")
private String cityName;
@Column(name = "city_id")
private Long cityId;
@Column(name = "store_code")
private String storeCode;
@Column(name = "oss_user_name")
private String ossUserName;
@Column(name = "remark")
private String remark;
@Column(name = "type")
private Integer type;
@Column(name = "service_count")
private Integer serviceCount;
@Column(name = "binding_count")
private Integer bindingCount;
@Column(name = "is_person")
private Boolean isPerson;
@Column(name = "is_car")
private Boolean isCar;
}
@EnableWinSecurity设置extensionRoleDTO、extensionRoleEntity
extensionRoleDTO 角色前端请求对象
@Data
public class BrokerageAdminRoleDTO extends WinSecurityBaseRoleDTO {
private Integer departmentLevel;
}
@Entity
@DynamicInsert
@DynamicUpdate
@Data
public class BrokerageAdminRoleEntity extends WinSecurityBaseRoleEntity {
@Column(name = "DEPARTMENT_LEVEL")
private Integer departmentLevel;
}
支持四种场景的业务扩展,需要实现特定的接口
例:
@Slf4j
public class UserAddProcessorImpl implements IUserAddProcessor<BrokerageAdminUserDTO, BrokerageAdminUserEntity> {
@Resource
private OrgDepartmentService orgDepartmentService;
@Resource
private BrokerageAdminService brokerageAdminService;
@Override
public void preProcess(BrokerageAdminUserDTO dto) throws WinSecurityException {
}
@Override
public void customValidateAfterCommon(BrokerageAdminUserDTO dto) throws WinSecurityException {
if (dto.getSubDepartmentId() == null) {
throw new WinSecurityException("未选择机构");
}
BrokerageOrgDepartment selectDepartment = orgDepartmentService.findById(dto.getSubDepartmentId());
if (!CollectionUtils.isEmpty(dto.getRoleIdList())) {
for (Long roleId : dto.getRoleIdList()) {
BrokerageAdminRoleDTO selectRole = brokerageAdminService.getRoleById(roleId);
if (!selectDepartment.getLevel().equals(selectRole.getDepartmentLevel())) {
throw new WinSecurityException("机构等级与角色等级不符");
}
}
}
}
@Override
public void customMappingAfterCommon(BrokerageAdminUserDTO dto, BrokerageAdminUserEntity entity) throws WinSecurityException {
if (StringUtils.isNotBlank(dto.getPassword())) {
entity.setPassword(DigestUtils.md5Hex(dto.getUserName() + dto.getPassword()));
}
}
@Override
public void postProcess(BrokerageAdminUserDTO dto) throws WinSecurityException {
}
}
compile("com.winbaoxian.module:win-security:1.0.0-SNAPSHOT")
compile('org.hibernate:hibernate-core:5.0.12.Final')
compile('org.aspectj:aspectjrt:1.8.13')
compile('org.aspectj:aspectjweaver:1.8.13')
使用jar包中security.sql创建相关表,用户、角色、资源及关系表
@Configuration
@EnableWinSecurity(transactionManagerRef = "transactionManagerWinSecurity", entityManagerFactoryRef = "entityManagerFactoryWinSecurity", tablePrefix = "security")
public class WinSecurityConfiguration {
@Resource
private DataSource dataSource;
@Resource
private SessionFactoryImpl sessionFactory;
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactoryWinSecurity() {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setPackagesToScan(new String[]{});
factoryBean.setPersistenceUnitName("winSecurity");
factoryBean.setJpaProperties(sessionFactory.getProperties());
factoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
return factoryBean;
}
@Bean
PlatformTransactionManager transactionManagerWinSecurity() {
return new JpaTransactionManager(entityManagerFactoryWinSecurity().getObject());
}
}
@EnableWinSecurity扩展方式 点击查看
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="detectHandlerMethodsInAncestorContexts">
<value>truevalue>
property>
bean>
WinSecurityAccessService.login(String userName);
WinSecurityAccessService.logout();
http://doc.suiyiwen.com/win-security/