satoken - 1
This commit is contained in:
@@ -15,9 +15,9 @@
|
||||
*/
|
||||
package org.nl.config;
|
||||
|
||||
import org.nl.utils.SecurityUtils;
|
||||
import org.springframework.data.domain.AuditorAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@@ -37,7 +37,7 @@ public class AuditorConfig implements AuditorAware<String> {
|
||||
public Optional<String> getCurrentAuditor() {
|
||||
try {
|
||||
// 这里应根据实际业务情况获取具体信息
|
||||
return Optional.of(SecurityUtils.getCurrentUsername());
|
||||
return Optional.of(null);
|
||||
}catch (Exception ignored){}
|
||||
// 用户定时任务,或者无Token调用的情况
|
||||
return Optional.of("System");
|
||||
|
||||
@@ -15,12 +15,7 @@
|
||||
*/
|
||||
package org.nl.config;
|
||||
|
||||
import org.nl.utils.SecurityUtils;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
@@ -30,8 +25,9 @@ public class ElPermissionConfig {
|
||||
|
||||
public Boolean check(String ...permissions){
|
||||
// 获取当前用户的所有权限
|
||||
List<String> elPermissions = SecurityUtils.getCurrentUser().getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
|
||||
// List<String> elPermissions = SecurityUtils.getCurrentUser().getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
|
||||
// 判断当前用户的所有权限是否包含接口上定义的权限
|
||||
return elPermissions.contains("admin") || Arrays.stream(permissions).anyMatch(elPermissions::contains);
|
||||
// return elPermissions.contains("admin") || Arrays.stream(permissions).anyMatch(elPermissions::contains);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,12 +22,13 @@ import org.nl.exception.EntityNotFoundException;
|
||||
import org.nl.utils.ThrowableUtil;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import java.util.Objects;
|
||||
import static org.springframework.http.HttpStatus.*;
|
||||
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
@@ -50,13 +51,13 @@ public class GlobalExceptionHandler {
|
||||
/**
|
||||
* BadCredentialsException
|
||||
*/
|
||||
@ExceptionHandler(BadCredentialsException.class)
|
||||
public ResponseEntity<ApiError> badCredentialsException(BadCredentialsException e){
|
||||
// 打印堆栈信息
|
||||
String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
|
||||
log.error(message);
|
||||
return buildResponseEntity(ApiError.error(message));
|
||||
}
|
||||
// @ExceptionHandler(BadCredentialsException.class)
|
||||
// public ResponseEntity<ApiError> badCredentialsException(BadCredentialsException e){
|
||||
// // 打印堆栈信息
|
||||
// String message = "坏的凭证".equals(e.getMessage()) ? "用户名或密码不正确" : e.getMessage();
|
||||
// log.error(message);
|
||||
// return buildResponseEntity(ApiError.error(message));
|
||||
// }
|
||||
|
||||
/**
|
||||
* 处理自定义异常
|
||||
|
||||
@@ -43,7 +43,8 @@ public class QueryHelp {
|
||||
DataPermission permission = query.getClass().getAnnotation(DataPermission.class);
|
||||
if(permission != null){
|
||||
// 获取数据权限
|
||||
List<Long> dataScopes = SecurityUtils.getCurrentUserDataScope();
|
||||
// List<Long> dataScopes = SecurityUtils.getCurrentUserDataScope();
|
||||
List<Long> dataScopes = new ArrayList<>();
|
||||
if(CollectionUtil.isNotEmpty(dataScopes)){
|
||||
if(StrUtil.isNotEmpty(permission.joinName()) && StrUtil.isNotEmpty(permission.fieldName())) {
|
||||
Join join = root.join(permission.joinName(), JoinType.LEFT);
|
||||
|
||||
@@ -1,59 +1,34 @@
|
||||
/*
|
||||
* Copyright 2019-2020 Zheng Jie
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.nl.utils;
|
||||
|
||||
import cn.hutool.json.JSONArray;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.exception.BadRequestException;
|
||||
import org.nl.utils.enums.DataScopeEnum;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
|
||||
import java.util.List;
|
||||
import org.nl.utils.dto.CurrentUser;
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户
|
||||
*
|
||||
* @author Zheng Jie
|
||||
* @date 2019-01-17
|
||||
* @author: lyd
|
||||
* @description: 获取当前用户的信息 - 前提下在登录之后将数据存储到session
|
||||
* @Date:
|
||||
*/
|
||||
@Slf4j
|
||||
public class SecurityUtils {
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户
|
||||
* 获取系统用户 - CurrentUser
|
||||
*
|
||||
* @return UserDetails
|
||||
* @return 系统用户
|
||||
*/
|
||||
public static UserDetails getCurrentUser() {
|
||||
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
if (authentication == null) {
|
||||
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "当前登录状态过期");
|
||||
public static Object getCurrentUser() {
|
||||
CurrentUser user = null;
|
||||
try {
|
||||
user = (CurrentUser) StpUtil.getTokenSession().get("userInfo");
|
||||
if (user.getUser() != null) {
|
||||
return user.getUser();
|
||||
}
|
||||
}
|
||||
if (authentication.getPrincipal() instanceof UserDetails) {
|
||||
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
||||
UserDetailsService userDetailsService = SpringContextHolder.getBean(UserDetailsService.class);
|
||||
return userDetailsService.loadUserByUsername(userDetails.getUsername());
|
||||
catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
throw new BadRequestException(HttpStatus.UNAUTHORIZED, "找不到当前登录的信息");
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,58 +37,23 @@ public class SecurityUtils {
|
||||
* @return 系统用户名称
|
||||
*/
|
||||
public static String getCurrentUsername() {
|
||||
try {
|
||||
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
|
||||
return userDetails.getUsername();
|
||||
} catch (Exception e) {
|
||||
return "auto";
|
||||
CurrentUser currentUser = (CurrentUser) StpUtil.getTokenSession().get("userInfo");
|
||||
if (currentUser.getUsername() != null) {
|
||||
return currentUser.getUsername();
|
||||
}
|
||||
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统用户ID
|
||||
* 获取系统用户Id
|
||||
*
|
||||
* @return 系统用户ID
|
||||
* @return 系统用户Id
|
||||
*/
|
||||
public static Long getCurrentUserId() {
|
||||
UserDetails userDetails = getCurrentUser();
|
||||
return new JSONObject(new JSONObject(userDetails).get("user")).get("id", Long.class);
|
||||
}
|
||||
/**
|
||||
* 获取系统用户昵称
|
||||
*
|
||||
* @return 系统用户ID
|
||||
*/
|
||||
public static String getNickName() {
|
||||
UserDetails userDetails = getCurrentUser();
|
||||
return new JSONObject(new JSONObject(userDetails).get("user")).get("nickName", String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户的数据权限
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
public static List<Long> getCurrentUserDataScope() {
|
||||
UserDetails userDetails = getCurrentUser();
|
||||
JSONArray array = JSONUtil.parseArray(new JSONObject(userDetails).get("dataScopes"));
|
||||
return JSONUtil.toList(array, Long.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据权限级别
|
||||
*
|
||||
* @return 级别
|
||||
*/
|
||||
public static String getDataScopeType() {
|
||||
List<Long> dataScopes = getCurrentUserDataScope();
|
||||
if (dataScopes.size() != 0) {
|
||||
return "";
|
||||
CurrentUser currentUser = (CurrentUser) StpUtil.getTokenSession().get("userInfo");
|
||||
if (currentUser.getId() != null) {
|
||||
return currentUser.getId();
|
||||
}
|
||||
return DataScopeEnum.ALL.getValue();
|
||||
return 0L;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package org.nl.utils.dto;
|
||||
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author: lyd
|
||||
* @description: 当前用户的信息
|
||||
* @Date:
|
||||
*/
|
||||
@Data
|
||||
public class CurrentUser implements Serializable {
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long id;
|
||||
|
||||
private String username;
|
||||
|
||||
private Object user;
|
||||
}
|
||||
Reference in New Issue
Block a user