diff --git a/inventory-dao/src/main/java/com/rzyc/mapper/EntPostDutyMapper.java b/inventory-dao/src/main/java/com/rzyc/mapper/EntPostDutyMapper.java index b52e32d..2df4875 100644 --- a/inventory-dao/src/main/java/com/rzyc/mapper/EntPostDutyMapper.java +++ b/inventory-dao/src/main/java/com/rzyc/mapper/EntPostDutyMapper.java @@ -23,8 +23,10 @@ public interface EntPostDutyMapper extends BaseMapper { * 企业用户岗位职责 * @param enterpriseId 企业id * @param postId 岗位id + * @param page 页码 + * @param pageSize 条数 * @return EntPostTask 企业用户岗位职责 * */ - ListselectEntUserPostDuty(@Param("enterpriseId") String enterpriseId, @Param("postId")String postId); + ListselectEntUserPostDuty(@Param("enterpriseId") String enterpriseId, @Param("postId")String postId,@Param("page") Integer page,@Param("pageSize")Integer pageSize); } diff --git a/inventory-dao/src/main/java/com/rzyc/mapper/EntPostListMapper.java b/inventory-dao/src/main/java/com/rzyc/mapper/EntPostListMapper.java index d9be173..8bc209d 100644 --- a/inventory-dao/src/main/java/com/rzyc/mapper/EntPostListMapper.java +++ b/inventory-dao/src/main/java/com/rzyc/mapper/EntPostListMapper.java @@ -23,8 +23,10 @@ public interface EntPostListMapper extends BaseMapper { * @param enterpriseId 企业id * @param entUserId 企业用户id * @param postId 岗位id + * @param page 页码 + * @param pageSize 条数 * @return EntPostList 企业用户工作要务 * */ - ListselectEntPostList(@Param("enterpriseId") String enterpriseId,@Param("entUserId") String entUserId,@Param("postId")String postId); + ListselectEntPostList(@Param("enterpriseId") String enterpriseId,@Param("entUserId") String entUserId,@Param("postId")String postId,Integer page,Integer pageSize); } diff --git a/inventory-dao/src/main/java/com/rzyc/mapper/EntPostTaskMapper.java b/inventory-dao/src/main/java/com/rzyc/mapper/EntPostTaskMapper.java index cf6e8da..0235d84 100644 --- a/inventory-dao/src/main/java/com/rzyc/mapper/EntPostTaskMapper.java +++ b/inventory-dao/src/main/java/com/rzyc/mapper/EntPostTaskMapper.java @@ -27,10 +27,13 @@ public interface EntPostTaskMapper extends BaseMapper { * @param listId 清单id * @param content 内容模糊搜索 * @param taskState 任务状态 + * @param page 页码 + * @param pageSize 条数 * @return EntPostTask 企业用户清单任务 * */ ListselectEntUserPostTask(@Param("enterpriseId") String enterpriseId, @Param("entUserId")String entUserId, @Param("postId")String postId, @Param("listId")String listId, - @Param("content")String content,@Param("taskState") Integer taskState); + @Param("content")String content,@Param("taskState") Integer taskState, + @Param("page") Integer page,@Param("pageSize") Integer pageSize); } diff --git a/inventory-dao/src/main/java/com/rzyc/mapper/EntUserCredentialMapper.java b/inventory-dao/src/main/java/com/rzyc/mapper/EntUserCredentialMapper.java index 52ac254..30cacec 100644 --- a/inventory-dao/src/main/java/com/rzyc/mapper/EntUserCredentialMapper.java +++ b/inventory-dao/src/main/java/com/rzyc/mapper/EntUserCredentialMapper.java @@ -24,25 +24,27 @@ public interface EntUserCredentialMapper extends BaseMapper { * 企业用户岗位职责 * @param enterpriseId 企业id * @param entUserId 企业用户id + * @param page 页码 + * @param pageSize 条数 * @return EntUserCredential 企业用户证照 * */ - ListselectEntUserCredential(@Param("enterpriseId") String enterpriseId, @Param("entUserId")String entUserId); + ListselectEntUserCredential(@Param("enterpriseId") String enterpriseId, @Param("entUserId")String entUserId,@Param("page") Integer page,@Param("pageSize")Integer pageSize); /** * 企业用户证件照修改 - * @param entUserCredentialUpdateDto 企业用户证件照对象 + * @param entUserCredential 企业用户证件照对象 * @return int * */ - int updateEntUserCredential(@Param("data") EntUserCredentialUpdateDto entUserCredentialUpdateDto); + int updateEntUserCredential(@Param("data") EntUserCredential entUserCredential); /** * 企业用户证件照新增 - * @param entUserCredentialUpdateDto 企业用户证件照对象 + * @param entUserCredential 企业用户证件照对象 * @return int * */ - int insertEntUserCredential(@Param("data") EntUserCredentialUpdateDto entUserCredentialUpdateDto); + int insertEntUserCredential(@Param("data") EntUserCredential entUserCredential); /** diff --git a/inventory-dao/src/main/java/com/rzyc/mapper/ent/EntUserMapper.java b/inventory-dao/src/main/java/com/rzyc/mapper/ent/EntUserMapper.java index 92e3d09..a287ee2 100644 --- a/inventory-dao/src/main/java/com/rzyc/mapper/ent/EntUserMapper.java +++ b/inventory-dao/src/main/java/com/rzyc/mapper/ent/EntUserMapper.java @@ -1,6 +1,5 @@ package com.rzyc.mapper.ent; -import com.rzyc.model.ent.EntPost; import com.rzyc.model.ent.EntUser; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Param; @@ -27,6 +26,14 @@ public interface EntUserMapper extends BaseMapper { EntUser selectByName(@Param("name") String name); + /** + * 查询企业用户表 + * @param keyContent 关键字 + * @param page 页码 + * @param pageSize 条数 + * @return EntUser 企业用户实体 + * */ + ListselectEntUserList(@Param("keyContent") String keyContent,@Param("page")Integer page,@Param("pageSize")Integer pageSize); } diff --git a/inventory-dao/src/main/java/com/rzyc/model/dto/EntUserCredentialUpdateDto.java b/inventory-dao/src/main/java/com/rzyc/model/dto/EntUserCredentialUpdateDto.java index 52cec7e..5074609 100644 --- a/inventory-dao/src/main/java/com/rzyc/model/dto/EntUserCredentialUpdateDto.java +++ b/inventory-dao/src/main/java/com/rzyc/model/dto/EntUserCredentialUpdateDto.java @@ -41,17 +41,6 @@ public class EntUserCredentialUpdateDto { private Date credentialEndTime; - private Date createTime; - - - private Date modifyTime; - - - private String createBy; - - - private String modifyBy; - public Date getCredentialStartTime() { return credentialStartTime; } @@ -96,34 +85,6 @@ public class EntUserCredentialUpdateDto { public void setCredentialImageAddress(String credentialImageAddress) { this.credentialImageAddress = credentialImageAddress; } - public Date getCreateTime() { - return createTime; - } - - public void setCreateTime(Date createTime) { - this.createTime = createTime; - } - public Date getModifyTime() { - return modifyTime; - } - - public void setModifyTime(Date modifyTime) { - this.modifyTime = modifyTime; - } - public String getCreateBy() { - return createBy; - } - - public void setCreateBy(String createBy) { - this.createBy = createBy; - } - public String getModifyBy() { - return modifyBy; - } - - public void setModifyBy(String modifyBy) { - this.modifyBy = modifyBy; - } public String getEntUserId() { return entUserId; diff --git a/inventory-dao/src/main/java/com/rzyc/model/ent/EntUser.java b/inventory-dao/src/main/java/com/rzyc/model/ent/EntUser.java index 696f913..a944da8 100644 --- a/inventory-dao/src/main/java/com/rzyc/model/ent/EntUser.java +++ b/inventory-dao/src/main/java/com/rzyc/model/ent/EntUser.java @@ -82,8 +82,79 @@ public class EntUser implements Serializable { @TableField("modify_by") private String modifyBy; + @ApiModelProperty(value = "证件未超时数") + @TableField(exist = false) + private Integer noTimeout; + @ApiModelProperty(value = "证件超时数") + @TableField(exist = false) + private Integer overtime; + + @ApiModelProperty(value = "进行中履职任务数") + @TableField(exist = false) + private Integer ongoingTask; + + @ApiModelProperty(value = "完成的履职任务数") + @TableField(exist = false) + private Integer finishTask; + + @ApiModelProperty(value = "超时的履职任务数") + @TableField(exist = false) + private Integer overTimeTask; + + @ApiModelProperty(value = "履职百分比数") + @TableField(exist = false) + private double entUserTaskPercent; + + public double getEntUserTaskPercent() { + return entUserTaskPercent; + } + + public void setEntUserTaskPercent(double entUserTaskPercent) { + this.entUserTaskPercent = entUserTaskPercent; + } + + public Integer getNoTimeout() { + return noTimeout; + } + + public void setNoTimeout(Integer noTimeout) { + this.noTimeout = noTimeout; + } + + public Integer getOvertime() { + return overtime; + } + + public void setOvertime(Integer overtime) { + this.overtime = overtime; + } + + public Integer getOngoingTask() { + return ongoingTask; + } + + public void setOngoingTask(Integer ongoingTask) { + this.ongoingTask = ongoingTask; + } + + public Integer getFinishTask() { + return finishTask; + } + + public void setFinishTask(Integer finishTask) { + this.finishTask = finishTask; + } + + public Integer getOverTimeTask() { + return overTimeTask; + } + + public void setOverTimeTask(Integer overTimeTask) { + this.overTimeTask = overTimeTask; + } + public String getEntUserId() { return entUserId; } diff --git a/inventory-dao/src/main/resources/mapper/EntPostDutyMapper.xml b/inventory-dao/src/main/resources/mapper/EntPostDutyMapper.xml index 3a6e1f3..cfd0032 100644 --- a/inventory-dao/src/main/resources/mapper/EntPostDutyMapper.xml +++ b/inventory-dao/src/main/resources/mapper/EntPostDutyMapper.xml @@ -21,7 +21,7 @@ diff --git a/inventory-dao/src/main/resources/mapper/EntUserCredentialMapper.xml b/inventory-dao/src/main/resources/mapper/EntUserCredentialMapper.xml index bea4e7b..3052c7a 100644 --- a/inventory-dao/src/main/resources/mapper/EntUserCredentialMapper.xml +++ b/inventory-dao/src/main/resources/mapper/EntUserCredentialMapper.xml @@ -22,19 +22,19 @@ credential_id, credential_name, credential_state, credential_image_address, create_time, modify_time, create_by, modify_by - + select * from ent_user_credential where ent_user_id = #{entUserId} limit #{page},#{pageSize} - + update ent_user_credential set credential_name= #{data.credentialName} ,credential_start_time = #{data.credentialStartTime}, credential_end_time = #{data.credentialEndTime},credential_image_address = #{data.credentialImageAddress}, - modify_timne = #{data.modifyTime} , modify_by = #{data.modifyBy} + modify_time = #{data.modifyTime} , modify_by = #{data.modifyBy} where ent_user_id = #{data.entUserId} - - insert into ent_user_credential (credential_name,credential_start_time,credential_end_time,credential_image_address,ent_user_id,create_by,create_time) values(#{data.credentialName},#{data.credentialStartTime},#{data.credentialEndTime},#{data.credentialImageAddress},#{data.entUserId},#{data.createBy},#{data.createTime}) + + insert into ent_user_credential (credential_id,credential_name,credential_start_time,credential_end_time,credential_image_address,ent_user_id,create_by,create_time) values(#{data.credentialId},#{data.credentialName},#{data.credentialStartTime},#{data.credentialEndTime},#{data.credentialImageAddress},#{data.entUserId},#{data.createBy},#{data.createTime}) diff --git a/inventory-dao/src/main/resources/mapper/ent/EntUserMapper.xml b/inventory-dao/src/main/resources/mapper/ent/EntUserMapper.xml index 729d5a7..66753cd 100644 --- a/inventory-dao/src/main/resources/mapper/ent/EntUserMapper.xml +++ b/inventory-dao/src/main/resources/mapper/ent/EntUserMapper.xml @@ -29,7 +29,46 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/inventory-ent/src/main/java/com/rzyc/config/MethodSignature.java b/inventory-ent/src/main/java/com/rzyc/config/EntMethodSignature.java similarity index 88% rename from inventory-ent/src/main/java/com/rzyc/config/MethodSignature.java rename to inventory-ent/src/main/java/com/rzyc/config/EntMethodSignature.java index 935965e..b924828 100644 --- a/inventory-ent/src/main/java/com/rzyc/config/MethodSignature.java +++ b/inventory-ent/src/main/java/com/rzyc/config/EntMethodSignature.java @@ -2,7 +2,6 @@ package com.rzyc.config; import com.common.utils.DateUtils; import com.common.utils.RandomNumber; -import com.rzyc.config.MethodAnnotation; import com.rzyc.controller.PersonalController; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; @@ -17,7 +16,7 @@ import java.sql.*; * @date 2022/10/8 */ -public class MethodSignature { +public class EntMethodSignature { public static void main(String[] args) { @@ -48,13 +47,10 @@ public class MethodSignature { //创建connection Connection con = buildingSource().getConnection(); Statement statement = con.createStatement(); - PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO `authority_key`(id,parent_resource,auth_key,category,create_time,modify_time) VALUES (?,?,?,?,?,?);"); + PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO `authority_key`(id,parent_resource,auth_key,category,create_time,modify_time,`name`) VALUES (?,?,?,?,?,?,?);"); con.setAutoCommit(false); long startTime = System.currentTimeMillis(); - if (methods.length>0){ - statement.execute("truncate table authority_key"); - } //遍历所有方法 for (Method m : methods) { //判断方法是否有MethodAnnotation注解 @@ -65,12 +61,11 @@ public class MethodSignature { for (String name : annotation.authorizations()) { String str = name.substring(name.indexOf(":")+1,name.length()); - /* ResultSet rs = statement.executeQuery("select auth_key from authority_key where auth_key ='"+str+"'"); + ResultSet rs = statement.executeQuery("select auth_key from authority_key where auth_key ='"+str+"'"); //取数据 if (rs.next()) { - } else {}*/ - + } else { String category = name.substring(0,name.indexOf(":")); preparedStatement.setString(1, RandomNumber.getUUid()); preparedStatement.setString(2,null); @@ -78,8 +73,9 @@ public class MethodSignature { preparedStatement.setString(4,category); preparedStatement.setString(5, DateUtils.getNowDateTimeStr()); preparedStatement.setString(6,DateUtils.getNowDateTimeStr()); + preparedStatement.setString(7,annotation.name()); preparedStatement.addBatch(); - + } } preparedStatement.executeBatch(); } diff --git a/inventory-ent/src/main/java/com/rzyc/config/MethodAnnotation.java b/inventory-ent/src/main/java/com/rzyc/config/MethodAnnotation.java index c5728bd..70db88a 100644 --- a/inventory-ent/src/main/java/com/rzyc/config/MethodAnnotation.java +++ b/inventory-ent/src/main/java/com/rzyc/config/MethodAnnotation.java @@ -19,5 +19,7 @@ public @interface MethodAnnotation { String authorization()default "no authorization"; + String name()default "no name"; + } diff --git a/inventory-ent/src/main/java/com/rzyc/controller/PersonalController.java b/inventory-ent/src/main/java/com/rzyc/controller/PersonalController.java index dfcdf98..73a943a 100644 --- a/inventory-ent/src/main/java/com/rzyc/controller/PersonalController.java +++ b/inventory-ent/src/main/java/com/rzyc/controller/PersonalController.java @@ -7,6 +7,7 @@ import com.common.utils.StringUtils; import com.common.utils.encryption.PasswdFactory; import com.common.utils.jwt.JwtUtil; import com.common.utils.model.SingleResult; +import com.rzyc.advice.PageOperation; import com.rzyc.bean.user.dto.LoginDto; import com.rzyc.model.EntUserCredential; import com.rzyc.model.dto.EntUserCredentialUpdateDto; @@ -159,7 +160,7 @@ public class PersonalController extends BaseController{ }) @GetMapping(value = "/entUserTree") @PreAuthorize("hasAnyAuthority('PERSONAL:ENTUSERTREE','PERSONAL:ENTUSERTREE:UPDATE')") - @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERTREE","PERSONAL:ENTUSERTREE:UPDATE"}) + @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERTREE","PERSONAL:ENTUSERTREE:UPDATE"},name = "企业用户组织树") @ResponseBody public SingleResult> entUserTree(String enterpriseId, String postId)throws Exception{ return pcBusinessService.entUserTree(enterpriseId,postId); @@ -182,10 +183,10 @@ public class PersonalController extends BaseController{ }) @GetMapping(value = "/entUserPostList") @PreAuthorize("hasAnyAuthority('PERSONAL:ENTUSERPOSTLIST','PERSONAL:ENTUSERPOSTLIST:UPDATE')") - @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERPOSTLIST","PERSONAL:ENTUSERPOSTLIST:UPDATE"}) + @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERPOSTLIST","PERSONAL:ENTUSERPOSTLIST:UPDATE"},name = "企业用户工作要务") @ResponseBody - public SingleResult entUserPostList(String enterpriseId, String entUserId,String postId)throws Exception{ - return pcBusinessService.entUserPostList(enterpriseId,entUserId,postId); + public SingleResult entUserPostList(String enterpriseId, String entUserId,String postId,Integer page,Integer pageSize)throws Exception{ + return pcBusinessService.entUserPostList(enterpriseId,entUserId,postId,page,pageSize); } @@ -210,10 +211,10 @@ public class PersonalController extends BaseController{ @GetMapping(value = "/entUserPostTask") @PreAuthorize("hasAnyAuthority('PERSONAL:ENTUSERPOSTTASK','PERSONAL:ENTUSERPOSTTASK:UPDATE')") - @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERPOSTTASK","PERSONAL:ENTUSERPOSTTASK:UPDATE"}) + @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERPOSTTASK","PERSONAL:ENTUSERPOSTTASK:UPDATE"},name = "企业用户工作清单") @ResponseBody - public SingleResult entUserPostTask(String enterpriseId, String entUserId,String postId,String listId,String content,Integer taskState)throws Exception{ - return pcBusinessService.entUserPostTask(enterpriseId,entUserId,postId,listId,content,taskState); + public SingleResult entUserPostTask(String enterpriseId, String entUserId,String postId,String listId,String content,Integer taskState,Integer page,Integer pageSize)throws Exception{ + return pcBusinessService.entUserPostTask(enterpriseId,entUserId,postId,listId,content,taskState,page,pageSize); } @@ -232,10 +233,10 @@ public class PersonalController extends BaseController{ @GetMapping(value = "/entUserPostDuty") @PreAuthorize("hasAnyAuthority('PERSONAL:POSTDUTY','PERSONAL:POSTDUTY:UPDATE')") - @MethodAnnotation(authorizations = {"PERSONAL:POSTDUTY","PERSONAL:POSTDUTY:UPDATE"}) + @MethodAnnotation(authorizations = {"PERSONAL:POSTDUTY","PERSONAL:POSTDUTY:UPDATE"},name = "企业用户岗位职责") @ResponseBody - public SingleResult entUserPostDuty(String enterpriseId, String postId)throws Exception{ - return pcBusinessService.entUserPostDuty(enterpriseId,postId); + public SingleResult entUserPostDuty(String enterpriseId, String postId,Integer page,Integer pageSize)throws Exception{ + return pcBusinessService.entUserPostDuty(enterpriseId,postId,page,pageSize); } /** @@ -250,10 +251,10 @@ public class PersonalController extends BaseController{ }) @GetMapping(value = "/entUserCredential") @PreAuthorize("hasAnyAuthority('PERSONAL:ENTUSERCREDENTIAL','PERSONAL:ENTUSERCREDENTIAL:UPDATE')") - @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERCREDENTIAL","PERSONAL:ENTUSERCREDENTIAL:UPDATE"}) + @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERCREDENTIAL","PERSONAL:ENTUSERCREDENTIAL:UPDATE"},name = "企业用户证照表") @ResponseBody - public SingleResult entUserCredential(String entUserId)throws Exception{ - return pcBusinessService.entUserCredential(null,entUserId); + public SingleResult entUserCredential(String entUserId,Integer page,Integer pageSize)throws Exception{ + return pcBusinessService.entUserCredential(null,entUserId,page,pageSize); } /** @@ -268,7 +269,7 @@ public class PersonalController extends BaseController{ }) @PostMapping(value = "/entUserCredentialUpdate") @PreAuthorize("hasAnyAuthority('PERSONAL:ENTUSERCREDENTIAL:UPDATE')") - @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERCREDENTIAL:UPDATE"}) + @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERCREDENTIAL:UPDATE"},name = "企业用户证照表-新增,修改") @ResponseBody public SingleResult entUserCredentialUpdate(@RequestBody EntUserCredentialUpdateDto entUserCredentialUpdateDto)throws Exception{ return pcBusinessService.entUserCredentialUpdate(entUserCredentialUpdateDto); @@ -283,11 +284,11 @@ public class PersonalController extends BaseController{ */ @ApiOperation(value = "企业用户证照表-删除", notes = "企业用户证照表-删除") @ApiImplicitParams({ - @ApiImplicitParam(name = "entUserId", value = "企业用户id",required = true, dataType = "string"), + @ApiImplicitParam(name = "credentialId", value = "证件照id",required = true, dataType = "string"), }) @PostMapping(value = "/entUserCredentialDelete") @PreAuthorize("hasAnyAuthority('PERSONAL:ENTUSERCREDENTIAL:DELETE')") - @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERCREDENTIAL:DELETE"}) + @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERCREDENTIAL:DELETE"},name = "企业用户证照表-删除") @ResponseBody public SingleResult entUserCredentialDelete(String credentialId)throws Exception{ return pcBusinessService.entUserCredentialDelete(credentialId); @@ -295,11 +296,22 @@ public class PersonalController extends BaseController{ /** - * 企业岗位总体信息 + * 企业岗位总体信息列表 * @param keyContent * @return list * @throws Exception */ + @ApiOperation(value = "企业岗位总体信息列表", notes = "企业岗位总体信息列表") + @ApiImplicitParams({ + @ApiImplicitParam(name = "keyContent", value = "搜索关键字",required = false, dataType = "string"), + }) + @GetMapping(value = "/entUserList") + @PreAuthorize("hasAnyAuthority('PERSONAL:ENTUSERLIST','PERSONAL:ENTUSERLIST:UPDATE')") + @MethodAnnotation(authorizations = {"PERSONAL:ENTUSERLIST","PERSONAL:ENTUSERLIST:UPDATE"},name ="企业岗位总体信息列表") + @ResponseBody + public SingleResult entUserList(String keyContent,Integer page,Integer pageSize)throws Exception{ + return pcBusinessService.entUserList(keyContent,page,pageSize); + } diff --git a/inventory-ent/src/main/java/com/rzyc/filter/JwtAuthenticationTokenFiler.java b/inventory-ent/src/main/java/com/rzyc/filter/JwtAuthenticationTokenFiler.java index 8acf85b..d1fa9db 100644 --- a/inventory-ent/src/main/java/com/rzyc/filter/JwtAuthenticationTokenFiler.java +++ b/inventory-ent/src/main/java/com/rzyc/filter/JwtAuthenticationTokenFiler.java @@ -60,6 +60,7 @@ public class JwtAuthenticationTokenFiler extends OncePerRequestFilter { for (AuthorityKey s:authorizations) { stringBuilder.append(s.getCategory() +":"+s.getAuthKey()); authority.add(new SimpleGrantedAuthority(stringBuilder.toString())); + stringBuilder.setLength(0); } // 获取权限信息封装到Authentication中 diff --git a/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java b/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java index ab9f74a..8a69be7 100644 --- a/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java +++ b/inventory-ent/src/main/java/com/rzyc/service/PcBusinessService.java @@ -3,9 +3,7 @@ package com.rzyc.service; import cn.jiguang.common.TimeUnit; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; -import com.common.utils.DateUtils; -import com.common.utils.StringUtils; -import com.common.utils.TypeConversion; +import com.common.utils.*; import com.common.utils.model.Code; import com.common.utils.model.Message; import com.common.utils.model.SingleResult; @@ -21,6 +19,7 @@ import com.rzyc.model.ent.EntPost; import com.rzyc.model.ent.EntUser; import com.rzyc.model.ent.SysEnterprise; +import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import java.util.*; @@ -80,48 +79,56 @@ public class PcBusinessService extends BaseController { } - public SingleResult entUserPostList(String enterpriseId,String entUserId,String postId){ + public SingleResult entUserPostList(String enterpriseId,String entUserId,String postId,Integer page,Integer pageSize){ SingleResult singleResult = new SingleResult(); - Listlist = entPostListMapper.selectEntPostList(enterpriseId,entUserId,postId); + page = pageSize * (page - 1); + Listlist = entPostListMapper.selectEntPostList(enterpriseId,entUserId,postId,page,pageSize); singleResult.setData(list); return singleResult; } - public SingleResult entUserPostTask(String enterpriseId, String entUserId,String postId,String listId,String content,Integer taskState){ + public SingleResult entUserPostTask(String enterpriseId, String entUserId,String postId,String listId,String content,Integer taskState,Integer page,Integer pageSize){ SingleResult singleResult = new SingleResult(); + page = pageSize * (page - 1); //EntPostTask - Listlist = entPostTaskMapper.selectEntUserPostTask(enterpriseId,entUserId,postId,listId,content,taskState); + Listlist = entPostTaskMapper.selectEntUserPostTask(enterpriseId,entUserId,postId,listId,content,taskState,page,pageSize); singleResult.setData(list); return singleResult; } - public SingleResult entUserPostDuty(String enterpriseId, String postId){ + public SingleResult entUserPostDuty(String enterpriseId, String postId,Integer page,Integer pageSize){ SingleResult singleResult = new SingleResult(); + page = pageSize * (page - 1); //EntPostDuty - Listlist = entPostDutyMapper.selectEntUserPostDuty(enterpriseId,postId); + Listlist = entPostDutyMapper.selectEntUserPostDuty(enterpriseId,postId,page,pageSize); singleResult.setData(list); return singleResult; } - public SingleResult entUserCredential(String enterpriseId, String entUserId){ + public SingleResult entUserCredential(String enterpriseId, String entUserId,Integer page,Integer pageSize){ SingleResult singleResult = new SingleResult(); - Listlist = entUserCredentialMapper.selectEntUserCredential(enterpriseId,entUserId); + page = pageSize * (page - 1); + Listlist = entUserCredentialMapper.selectEntUserCredential(enterpriseId,entUserId,page,pageSize); + singleResult.setData(list); return singleResult; } public SingleResult entUserCredentialUpdate(EntUserCredentialUpdateDto entUserCredentialUpdateDto) throws Exception { SingleResult singleResult = new SingleResult(); + EntUserCredential entUserCredential = new EntUserCredential(); + BeanUtils.copyProperties(entUserCredentialUpdateDto,entUserCredential); Integer result = 0; - if (StringUtils.isNotBlank(entUserCredentialUpdateDto.getCredentialId())) { - entUserCredentialUpdateDto.setModifyBy(getUserId()); - entUserCredentialUpdateDto.setModifyTime(new Date()); - result = entUserCredentialMapper.updateEntUserCredential(entUserCredentialUpdateDto); + if (StringUtils.isNotBlank(entUserCredential.getCredentialId())) { + entUserCredential.setModifyBy(getUserId()); + entUserCredential.setModifyTime(new Date()); + result = entUserCredentialMapper.updateEntUserCredential(entUserCredential); }else { - entUserCredentialUpdateDto.setCreateBy(getUserId()); - entUserCredentialUpdateDto.setCreateTime(new Date()); - result = entUserCredentialMapper.insertEntUserCredential(entUserCredentialUpdateDto); + entUserCredential.setCredentialId(RandomNumber.getUUid()); + entUserCredential.setCreateBy(getUserId()); + entUserCredential.setCreateTime(new Date()); + result = entUserCredentialMapper.insertEntUserCredential(entUserCredential); } if (result==1){ singleResult.setCode(Code.SUCCESS.getCode()); @@ -141,6 +148,21 @@ public class PcBusinessService extends BaseController { } + public SingleResult entUserList(String keyContent,Integer page,Integer pageSize){ + SingleResult singleResult = new SingleResult(); + page = pageSize * (page - 1); + Listusers = entUserMapper.selectEntUserList(keyContent,page,pageSize); + //计算履职百分比,后期这里使用redis来读取履职进度 + for (EntUser e:users) { + Integer total = e.getFinishTask() + e.getOngoingTask() + e.getOverTimeTask(); + double percent = Arith.div(total,e.getFinishTask()) * 100; + e.setEntUserTaskPercent(percent); + } + singleResult.setData(users); + return singleResult; + } + + } diff --git a/inventory-ent/src/main/java/com/rzyc/config/UserDetailsServiceImpl.java b/inventory-ent/src/main/java/com/rzyc/service/UserDetailsServiceImpl.java similarity index 97% rename from inventory-ent/src/main/java/com/rzyc/config/UserDetailsServiceImpl.java rename to inventory-ent/src/main/java/com/rzyc/service/UserDetailsServiceImpl.java index 18492e5..ec571e4 100644 --- a/inventory-ent/src/main/java/com/rzyc/config/UserDetailsServiceImpl.java +++ b/inventory-ent/src/main/java/com/rzyc/service/UserDetailsServiceImpl.java @@ -1,5 +1,6 @@ -package com.rzyc.config; +package com.rzyc.service; +import com.rzyc.config.EntUserDetails; import com.rzyc.mapper.AuthorityKeyMapper; import com.rzyc.mapper.ent.EntUserMapper; import com.rzyc.model.AuthorityKey; diff --git a/inventory-gov/pom.xml b/inventory-gov/pom.xml index 01491bc..43591b2 100644 --- a/inventory-gov/pom.xml +++ b/inventory-gov/pom.xml @@ -209,6 +209,12 @@ spring-security-web 5.7.3 + + org.projectlombok + lombok + RELEASE + compile + diff --git a/inventory-gov/src/main/java/com/rzyc/advice/ExceptionAdvice.java b/inventory-gov/src/main/java/com/rzyc/advice/ExceptionAdvice.java index ba39d2a..5f20e63 100644 --- a/inventory-gov/src/main/java/com/rzyc/advice/ExceptionAdvice.java +++ b/inventory-gov/src/main/java/com/rzyc/advice/ExceptionAdvice.java @@ -7,6 +7,7 @@ import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; +import org.springframework.security.access.AccessDeniedException; import org.springframework.validation.BindException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -162,4 +163,19 @@ public class ExceptionAdvice { result.setMessage(e.getMessage()); return result; } + + /** + * @author Xuwanxin + * 权限不足总捕获异常 + * */ + @ResponseBody + @ExceptionHandler(value = AccessDeniedException.class) + public SingleResult handleAccessRE(AccessDeniedException e) { + e.printStackTrace(); + logger.info("权限不足"); + SingleResult result = new SingleResult<>(); + result.setCode(Code.AUTHORIZATION_FAILED.getCode()); + result.setMessage(Message.AUTHORIZATION_FAILED); + return result; + } } diff --git a/inventory-gov/src/main/java/com/rzyc/config/EntUserDetails.java b/inventory-gov/src/main/java/com/rzyc/config/EntUserDetails.java new file mode 100644 index 0000000..a8beed6 --- /dev/null +++ b/inventory-gov/src/main/java/com/rzyc/config/EntUserDetails.java @@ -0,0 +1,33 @@ +package com.rzyc.config; + +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.userdetails.User; + +import java.util.Collection; + + +/** + * spring security UserDetails Custom Part + * @author Xuwanxin + * @date 2022/9/28 + * */ + +public class EntUserDetails extends User { + + + private String id; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public EntUserDetails(String username, String password, Collection authorities, String id) { + super(username, password, authorities); + setId(id); + } + +} diff --git a/inventory-gov/src/main/java/com/rzyc/config/GovMethodSignature.java b/inventory-gov/src/main/java/com/rzyc/config/GovMethodSignature.java new file mode 100644 index 0000000..7063c54 --- /dev/null +++ b/inventory-gov/src/main/java/com/rzyc/config/GovMethodSignature.java @@ -0,0 +1,95 @@ +package com.rzyc.config; + +import com.common.utils.DateUtils; +import com.common.utils.RandomNumber; +import com.rzyc.controller.EmergencyController; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +import java.lang.reflect.Method; +import java.sql.*; + +/** + * 工具 + * 获取方法中的注解参数,插入数据库 + * @author Xuwanxin + * @date 2022/10/8 + */ + +public class GovMethodSignature { + + + public static void main(String[] args) { + //反射controller获取所有方法 + Method[] methods = EmergencyController.class.getMethods(); + insertAnnotation(methods); + + } + + private static HikariDataSource buildingSource() { + //配置文件 + HikariConfig hikariConfig = new HikariConfig(); + //mysql + hikariConfig.setJdbcUrl("jdbc:mysql://121.40.106.103:3306/inventory_db?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=false"); + hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver"); + hikariConfig.setUsername("rzyc"); + hikariConfig.setPassword("admin@rzyc2022.com##"); + hikariConfig.addDataSourceProperty("minimumIdle", "3"); + hikariConfig.addDataSourceProperty("maximumPoolSize", "10"); + hikariConfig.addDataSourceProperty("maxLifetime", "30000"); + HikariDataSource ds = new HikariDataSource(hikariConfig); + return ds; + } + + private static void insertAnnotation(Method[] methods) { + + try { + //创建connection + Connection con = buildingSource().getConnection(); + Statement statement = con.createStatement(); + PreparedStatement preparedStatement = con.prepareStatement("INSERT INTO `authority_key`(id,parent_resource,auth_key,category,create_time,modify_time,`name`) VALUES (?,?,?,?,?,?,?);"); + + con.setAutoCommit(false); + long startTime = System.currentTimeMillis(); + //遍历所有方法 + for (Method m : methods) { + //判断方法是否有MethodAnnotation注解 + if (m.isAnnotationPresent(MethodAnnotation.class)) { + + MethodAnnotation annotation = m.getAnnotation(MethodAnnotation.class); + + + for (String name : annotation.authorizations()) { + String str = name.substring(name.indexOf(":")+1,name.length()); + ResultSet rs = statement.executeQuery("select auth_key from authority_key where auth_key ='"+str+"'"); + //取数据 + if (rs.next()) { + + } else { + String category = name.substring(0,name.indexOf(":")); + preparedStatement.setString(1, RandomNumber.getUUid()); + preparedStatement.setString(2,null); + preparedStatement.setString(3,str); + preparedStatement.setString(4,category); + preparedStatement.setString(5, DateUtils.getNowDateTimeStr()); + preparedStatement.setString(6,DateUtils.getNowDateTimeStr()); + preparedStatement.setString(7,annotation.name()); + preparedStatement.addBatch(); + } + } + preparedStatement.executeBatch(); + } + } + long endTime = System.currentTimeMillis(); + con.commit(); + System.out.println("用时:" + (endTime-startTime)); + + //关闭connection + con.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + +} diff --git a/inventory-gov/src/main/java/com/rzyc/config/MethodAnnotation.java b/inventory-gov/src/main/java/com/rzyc/config/MethodAnnotation.java new file mode 100644 index 0000000..70db88a --- /dev/null +++ b/inventory-gov/src/main/java/com/rzyc/config/MethodAnnotation.java @@ -0,0 +1,25 @@ +package com.rzyc.config; + + +import java.lang.annotation.*; + +/** + * 注解参数,插入数据库 + * @author Xuwanxin + * @date 2022/10/8 + * */ + +@Documented +@Retention(RetentionPolicy.RUNTIME) +@Inherited +@Target(ElementType.METHOD)//注解作用于方法 +public @interface MethodAnnotation { + + String[] authorizations()default {"no authorization"}; + + String authorization()default "no authorization"; + + String name()default "no name"; + + +} diff --git a/inventory-gov/src/main/java/com/rzyc/config/SecurityConfig.java b/inventory-gov/src/main/java/com/rzyc/config/SecurityConfig.java new file mode 100644 index 0000000..2304e0a --- /dev/null +++ b/inventory-gov/src/main/java/com/rzyc/config/SecurityConfig.java @@ -0,0 +1,92 @@ +package com.rzyc.config; + +import com.rzyc.filter.JwtAuthenticationTokenFiler; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.authentication.AuthenticationManager; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +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.config.http.SessionCreationPolicy; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; + +/** + * spring security config + * @author Xuwanxin + * @date 2022/9/26 + * */ +@Configuration +@EnableWebSecurity +@EnableGlobalMethodSecurity(prePostEnabled = true) +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + /** + * 数据库用户连接 + */ + private UserDetailsService userService; + + + /** + * token jwt 验证拦截器 + * */ + private JwtAuthenticationTokenFiler jwtAuthenticationTokenFiler; + + @Autowired + public void setSecurityConfigFinder(UserDetailsService userService,JwtAuthenticationTokenFiler jwtAuthenticationTokenFiler) { + this.userService = userService; + this.jwtAuthenticationTokenFiler = jwtAuthenticationTokenFiler; + } + + + @Bean + public PasswordEncoder getPasswordEncoder() { + return new BCryptPasswordEncoder(); + } + + /** + * 暴露AuthenticationManager,存上下文 + * */ + @Bean + @Override + public AuthenticationManager authenticationManagerBean() throws Exception + { + return super.authenticationManagerBean(); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + // 对于登录接口 允许匿名访问 + .antMatchers("/personal/login","/personal/entlogin").anonymous() + //放行swagger + .antMatchers("/swagger-ui.html","/swagger-resources/**","/webjars/**","/v2/**","/api/**").permitAll() + // 除上面外的所有请求全部需要鉴权认证,配置退出路径 + .anyRequest().authenticated() + .and() + .logout().logoutUrl( "/logout") + .and() + //关闭security默认登陆框 + .formLogin().disable() + //关闭csrf + .csrf().disable() + //不通过Session获取SecurityContext + .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) + .and().addFilterBefore(jwtAuthenticationTokenFiler, UsernamePasswordAuthenticationFilter.class) + ; + } + + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + // 配置数据库访问,认证步骤 + auth.userDetailsService(userService); + } + +} diff --git a/inventory-gov/src/main/java/com/rzyc/controller/EmergencyController.java b/inventory-gov/src/main/java/com/rzyc/controller/EmergencyController.java index e393bd7..9e39763 100644 --- a/inventory-gov/src/main/java/com/rzyc/controller/EmergencyController.java +++ b/inventory-gov/src/main/java/com/rzyc/controller/EmergencyController.java @@ -16,6 +16,8 @@ import com.rzyc.bean.OthResourceDo; import com.rzyc.bean.OthTeamDo; import com.rzyc.bean.house.dto.*; import com.rzyc.bean.house.vo.*; + +import com.rzyc.config.MethodAnnotation; import com.rzyc.enums.BackType; import com.rzyc.enums.DelState; import com.rzyc.enums.RevertType; @@ -28,6 +30,7 @@ import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import org.apache.commons.beanutils.BeanUtils; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; @@ -620,6 +623,8 @@ public class EmergencyController extends BaseController { * */ @ApiOperation(value = "事故类型", notes = "事故类型") @GetMapping("/accidentType") + @PreAuthorize("hasAnyAuthority('PCEMERGENCY:ACCIDENTTYPE','PCEMERGENCY:ACCIDENTTYPE:UPDATE')") + @MethodAnnotation(authorizations = {"PCEMERGENCY:ACCIDENTTYPE","PCEMERGENCY:ACCIDENTTYPE:UPDATE"},name = "事故类型") @ResponseBody public SingleResult accidentType()throws Exception{ SingleResult singleResult = new SingleResult(); diff --git a/inventory-gov/src/main/java/com/rzyc/filter/JwtAuthenticationTokenFiler.java b/inventory-gov/src/main/java/com/rzyc/filter/JwtAuthenticationTokenFiler.java new file mode 100644 index 0000000..6a21968 --- /dev/null +++ b/inventory-gov/src/main/java/com/rzyc/filter/JwtAuthenticationTokenFiler.java @@ -0,0 +1,82 @@ +package com.rzyc.filter; + +import com.common.utils.jwt.JwtUtil; +import com.rzyc.advice.CustomException; +import com.rzyc.mapper.AuthorityKeyMapper; +import com.rzyc.model.AuthorityKey; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; +import org.springframework.web.filter.OncePerRequestFilter; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * spring security JWT Filter + * @author Xuwanxin + * @date 2022/9/26 + * */ + +@Component +@Slf4j +public class JwtAuthenticationTokenFiler extends OncePerRequestFilter { + @Autowired + AuthorityKeyMapper authorityKeyMapper; + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { + //获取token + String token = request.getHeader("userToken"); + if (!StringUtils.hasText(token)) { + //放行 + filterChain.doFilter(request, response); + return; + } + + try { + String userId = JwtUtil.getTokenMsg(token); + } catch (Exception e) { + System.out.println("token非法"); + throw new RuntimeException("token非法"); + } + try { + + Listauthorizations = authorityKeyMapper.allAuthorizations(); + List authority= new ArrayList(); + StringBuilder stringBuilder = new StringBuilder(); + for (AuthorityKey s:authorizations) { + stringBuilder.append(s.getCategory() +":"+s.getAuthKey()); + authority.add(new SimpleGrantedAuthority(stringBuilder.toString())); + stringBuilder.setLength(0); + } + + // 获取权限信息封装到Authentication中 + UsernamePasswordAuthenticationToken authenticationToken = + new UsernamePasswordAuthenticationToken(null,null,authority); + SecurityContextHolder.getContext().setAuthentication(authenticationToken); + + + + filterChain.doFilter(request, response); + }catch (AccessDeniedException e){ + System.out.println("权限失败"); + throw new CustomException("无权限操作"); + }catch (Exception e){ + e.printStackTrace(); + } + + } + +} diff --git a/inventory-gov/src/main/java/com/rzyc/service/UserDetailsServiceImpl.java b/inventory-gov/src/main/java/com/rzyc/service/UserDetailsServiceImpl.java new file mode 100644 index 0000000..8efadc2 --- /dev/null +++ b/inventory-gov/src/main/java/com/rzyc/service/UserDetailsServiceImpl.java @@ -0,0 +1,71 @@ +package com.rzyc.service; + +import com.rzyc.config.EntUserDetails; +import com.rzyc.mapper.AuthorityKeyMapper; +import com.rzyc.mapper.ent.EntUserMapper; +import com.rzyc.model.AuthorityKey; +import com.rzyc.model.ent.EntUser; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +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.password.PasswordEncoder; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * spring security 用户认证过程 + * @author Xuwanxin + * @author 2022/09/27 + * */ + +@Service("userService") +public class UserDetailsServiceImpl implements UserDetailsService { + + + + /** + * 内存过程密码加密 + * */ + private PasswordEncoder passwordEncoder; + + /** + * 企业端用户 + * */ + private EntUserMapper entUserMapper; + + private AuthorityKeyMapper authorityKeyMapper; + + @Autowired + public void UserDetailsServiceImplFinder(PasswordEncoder passwordEncoder,EntUserMapper entUserMapper,AuthorityKeyMapper authorityKeyMapper) { + this.passwordEncoder = passwordEncoder; + this.entUserMapper = entUserMapper; + this.authorityKeyMapper = authorityKeyMapper; + } + + + + + @Override + public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException { + //判断数据库用户 + EntUser entUser = entUserMapper.selectByName(name); + if (Objects.isNull(entUser)){ + throw new UsernameNotFoundException("用户名或密码错误"); + } + List authority= new ArrayList(); + Listauthorizations = authorityKeyMapper.allAuthorizations(); + StringBuilder stringBuilder = new StringBuilder(); + for (AuthorityKey s:authorizations) { + stringBuilder.append(s.getCategory() +":"+s.getAuthKey()); + authority.add(new SimpleGrantedAuthority(stringBuilder.toString())); + } + return new EntUserDetails(entUser.getName(), passwordEncoder.encode(entUser.getPasswd()), authority,entUser.getEntUserId()); + + } +} diff --git a/inventory-gov/src/main/java/com/rzyc/service/UserLoginService.java b/inventory-gov/src/main/java/com/rzyc/service/UserLoginService.java new file mode 100644 index 0000000..dd52e78 --- /dev/null +++ b/inventory-gov/src/main/java/com/rzyc/service/UserLoginService.java @@ -0,0 +1,62 @@ +package com.rzyc.service; + + +import com.common.utils.jwt.JwtUtil; +import com.rzyc.config.EntUserDetails; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.authentication.BadCredentialsException; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.context.SecurityContextHolder; +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.password.PasswordEncoder; +import org.springframework.stereotype.Service; + +import java.util.Objects; + +/** + * 用户登陆 Service + * @author Xuwanxin + * @date 2022/9/26 + * */ + +@Service +public class UserLoginService { + + + private UserDetailsService userDetailsService; + + + private PasswordEncoder passwordEncoder; + + @Autowired + public void UserLoginServiceFinder(UserDetailsService userDetailsService,PasswordEncoder passwordEncoder){ + this.userDetailsService = userDetailsService; + this.passwordEncoder = passwordEncoder; + } + + public String login(String username, String password) { + String token = null; + try { + UserDetails userDetails = userDetailsService.loadUserByUsername(username); + if (Objects.isNull(userDetails)) { + throw new UsernameNotFoundException("账号不存在"); + } + //这里可能会不对,因为我们是MD5,这个是spring security 中的 encoder加密 + if (!passwordEncoder.matches(password, userDetails.getPassword())) { + throw new BadCredentialsException("密码不正确"); + } + //spring security context insert + UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); + SecurityContextHolder.getContext().setAuthentication(authentication); + //企业用户id + String id = ((EntUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId(); + token = JwtUtil.createToken(id); + } catch (AuthenticationException e) { + e.printStackTrace(); + } + return token; + } +}