diff --git a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/Authentication.java b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/Authentication.java
index ba9cdf72b..1dfd7cf21 100644
--- a/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/Authentication.java
+++ b/hsweb-authorization/hsweb-authorization-api/src/main/java/org/hswebframework/web/authorization/Authentication.java
@@ -20,6 +20,7 @@ package org.hswebframework.web.authorization;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
+import java.util.NoSuchElementException;
import java.util.Optional;
/**
@@ -37,6 +38,21 @@ import java.util.Optional;
*/
public interface Authentication extends Serializable {
+ /**
+ * 获取当前登录的用户权限信息
+ *
+ *
+ * Authentication auth= Authentication.current().get();
+ * //如果权限信息不存在将抛出{@link NoSuchElementException}建议使用下面的方式获取
+ * Authentication auth=Authentication.current().orElse(null);
+ * //或者
+ * Authentication auth=Authentication.current().orElseThrow(AuthorizeException::new);
+ *
+ *
+ * @return 返回Optional对象进行操作
+ * @see Optional
+ * @see AuthenticationHolder
+ */
static Optional current() {
return Optional.ofNullable(AuthenticationHolder.get());
}
diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-server/hsweb-authorization-oauth2-server-controller/src/main/java/org/hswebframework/web/authorization/oauth2/controller/OAuth2AuthorizationController.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-server/hsweb-authorization-oauth2-server-controller/src/main/java/org/hswebframework/web/authorization/oauth2/controller/OAuth2AuthorizationController.java
index 2a5a8dac8..0a07db186 100644
--- a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-server/hsweb-authorization-oauth2-server-controller/src/main/java/org/hswebframework/web/authorization/oauth2/controller/OAuth2AuthorizationController.java
+++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-server/hsweb-authorization-oauth2-server-controller/src/main/java/org/hswebframework/web/authorization/oauth2/controller/OAuth2AuthorizationController.java
@@ -21,8 +21,8 @@ package org.hswebframework.web.authorization.oauth2.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.binary.Base64;
+import org.hswebframework.web.AuthorizeException;
import org.hswebframework.web.authorization.Authentication;
-import org.hswebframework.web.authorization.AuthenticationHolder;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.oauth2.api.OAuth2ServerService;
import org.hswebframework.web.authorization.oauth2.model.AccessTokenModel;
@@ -55,7 +55,7 @@ public class OAuth2AuthorizationController {
@RequestParam("redirect_uri") String redirectUri,
@RequestParam(value = "scope", required = false) String scope,
@RequestParam(value = "state", required = false) String state) {
- Authentication authentication = AuthenticationHolder.get();
+ Authentication authentication = Authentication.current().orElseThrow(AuthorizeException::new);
String code = oAuth2ServerService.requestCode(clientId, authentication.getUser().getId(), scope);
AuthorizationCodeModel model = new AuthorizationCodeModel();
model.setCode(code);
diff --git a/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/DataAccessAnnotationMethodInterceptor.java b/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/DataAccessAnnotationMethodInterceptor.java
index 3f887af18..8a4b006d6 100644
--- a/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/DataAccessAnnotationMethodInterceptor.java
+++ b/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/DataAccessAnnotationMethodInterceptor.java
@@ -22,8 +22,8 @@ import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.aop.AuthorizingAnnotationHandler;
import org.apache.shiro.authz.aop.AuthorizingAnnotationMethodInterceptor;
import org.hswebframework.web.ApplicationContextHolder;
+import org.hswebframework.web.AuthorizeException;
import org.hswebframework.web.authorization.Authentication;
-import org.hswebframework.web.authorization.AuthenticationHolder;
import org.hswebframework.web.authorization.Permission;
import org.hswebframework.web.authorization.access.DataAccessConfig;
import org.hswebframework.web.authorization.access.DataAccessController;
@@ -49,12 +49,12 @@ import java.util.stream.Collectors;
* @author zhouhao
* @see DefaultDataAccessController
* @see DataAccessAnnotationHandler#assertAuthorized(Annotation)
- * @since 3.0
+ * @since 3.0
*/
public class DataAccessAnnotationMethodInterceptor extends AuthorizingAnnotationMethodInterceptor {
- public DataAccessAnnotationMethodInterceptor(DataAccessController controller,AnnotationResolver resolver) {
- super(new DataAccessAnnotationHandler(controller),resolver);
+ public DataAccessAnnotationMethodInterceptor(DataAccessController controller, AnnotationResolver resolver) {
+ super(new DataAccessAnnotationHandler(controller), resolver);
}
private static final Logger logger = LoggerFactory.getLogger(DataAccessAnnotationMethodInterceptor.class);
@@ -78,10 +78,7 @@ public class DataAccessAnnotationMethodInterceptor extends AuthorizingAnnotation
return;
}
//无权限信息
- Authentication authentication = AuthenticationHolder.get();
- if (authentication == null) {
- throw new AuthorizationException("{no_authorization}");
- }
+ Authentication authentication = Authentication.current().orElseThrow(AuthorizeException::new);
RequiresDataAccess accessAnn = ((RequiresDataAccess) a);
DataAccessController accessController = dataAccessController;
//在注解上自定义的权限控制器
diff --git a/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/FieldAccessAnnotationMethodInterceptor.java b/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/FieldAccessAnnotationMethodInterceptor.java
index 6c8c14796..cc067b9f2 100644
--- a/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/FieldAccessAnnotationMethodInterceptor.java
+++ b/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/FieldAccessAnnotationMethodInterceptor.java
@@ -43,8 +43,8 @@ import java.util.stream.Collectors;
*/
public class FieldAccessAnnotationMethodInterceptor extends AuthorizingAnnotationMethodInterceptor {
- public FieldAccessAnnotationMethodInterceptor(FieldAccessController controller,AnnotationResolver resolver) {
- super(new DataAccessAnnotationHandler(controller),resolver);
+ public FieldAccessAnnotationMethodInterceptor(FieldAccessController controller, AnnotationResolver resolver) {
+ super(new DataAccessAnnotationHandler(controller), resolver);
}
private static final Logger logger = LoggerFactory.getLogger(FieldAccessAnnotationMethodInterceptor.class);
@@ -67,10 +67,8 @@ public class FieldAccessAnnotationMethodInterceptor extends AuthorizingAnnotatio
}
RequiresFieldAccess accessAnn = ((RequiresFieldAccess) a);
MethodInterceptorParamContext context = holder.createParamContext();
- Authentication authentication = AuthenticationHolder.get();
- if (authentication == null) {
- throw new AuthorizationException("{no_authorization}");
- }
+ Authentication authentication = Authentication.current().orElseThrow(AuthorizationException::new);
+
String permission = accessAnn.permission();
Permission permissionInfo = authentication.getPermission(permission);
diff --git a/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/SimpleAuthorizeMethodInterceptor.java b/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/SimpleAuthorizeMethodInterceptor.java
index 28abe9ee6..40094d155 100644
--- a/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/SimpleAuthorizeMethodInterceptor.java
+++ b/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/SimpleAuthorizeMethodInterceptor.java
@@ -80,8 +80,8 @@ public class SimpleAuthorizeMethodInterceptor extends AuthorizingAnnotationMetho
}
authorizeConfig.put(authorize);
- Authentication authentication = AuthenticationHolder.get();
- if (null == authentication) throw new UnauthenticatedException(authorizeConfig.message);
+ Authentication authentication = Authentication.current()
+ .orElseThrow(() -> new UnauthenticatedException(authorizeConfig.message));
boolean access = true;
Logical logical = authorizeConfig.logical == Logical.DEFAULT ? Logical.OR : authorizeConfig.logical;
boolean logicalIsOr = logical == Logical.OR;
diff --git a/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/handler/OwnCreatedDataAccessHandler.java b/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/handler/OwnCreatedDataAccessHandler.java
index 481b5430f..70cbd3adc 100644
--- a/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/handler/OwnCreatedDataAccessHandler.java
+++ b/hsweb-authorization/hsweb-authorization-shiro/src/main/java/org/hswebframework/web/authorization/shiro/boost/handler/OwnCreatedDataAccessHandler.java
@@ -1,7 +1,8 @@
package org.hswebframework.web.authorization.shiro.boost.handler;
import org.hsweb.ezorm.core.param.Term;
-import org.hswebframework.web.authorization.AuthenticationHolder;
+import org.hswebframework.web.AuthorizeException;
+import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.authorization.Permission;
import org.hswebframework.web.authorization.access.DataAccessConfig;
import org.hswebframework.web.authorization.access.DataAccessHandler;
@@ -64,7 +65,9 @@ public class OwnCreatedDataAccessHandler implements DataAccessHandler {
.map(RecordCreationEntity.class::cast)
.findAny().orElse(null);
if (entity != null) {
- entity.setCreatorId(AuthenticationHolder.get().getUser().getId());
+ entity.setCreatorId(Authentication.current()
+ .orElseThrow(AuthorizeException::new)
+ .getUser().getId());
} else {
logger.warn("try put creatorId property,but not found any RecordCreationEntity!");
}
@@ -85,7 +88,7 @@ public class OwnCreatedDataAccessHandler implements DataAccessHandler {
QueryService queryService =
((QueryController) controller).getService();
RecordCreationEntity oldData = queryService.selectByPk(id);
- if (oldData != null && !AuthenticationHolder.get().getUser().getId().equals(oldData.getCreatorId())) {
+ if (oldData != null && !Authentication.current().orElseThrow(AuthorizeException::new).getUser().getId().equals(oldData.getCreatorId())) {
return false;
}
}
@@ -113,11 +116,11 @@ public class OwnCreatedDataAccessHandler implements DataAccessHandler {
queryParamEntity.setTerms(new ArrayList<>());
//添加一个查询条件
queryParamEntity
- .where(RecordCreationEntity.creatorId, AuthenticationHolder.get().getUser().getId())
+ .where(RecordCreationEntity.creatorId,Authentication.current().orElseThrow(AuthorizeException::new).getUser().getId())
//客户端提交的参数 作为嵌套参数
.nest().setTerms(oldParam);
} else if (entity instanceof RecordCreationEntity) {
- ((RecordCreationEntity) entity).setCreatorId(AuthenticationHolder.get().getUser().getId());
+ ((RecordCreationEntity) entity).setCreatorId(Authentication.current().orElseThrow(AuthorizeException::new).getUser().getId());
} else {
logger.warn("try validate query access,but entity not support, QueryParamEntity and RecordCreationEntity support now!");
}
diff --git a/hsweb-core/src/main/java/org/hswebframework/web/AuthorizeException.java b/hsweb-core/src/main/java/org/hswebframework/web/AuthorizeException.java
index 1e6fc39bc..3b130e07c 100644
--- a/hsweb-core/src/main/java/org/hswebframework/web/AuthorizeException.java
+++ b/hsweb-core/src/main/java/org/hswebframework/web/AuthorizeException.java
@@ -21,6 +21,10 @@ package org.hswebframework.web;
public class AuthorizeException extends BusinessException {
private static final long serialVersionUID = 2422918455013900645L;
+ public AuthorizeException() {
+ this("{no_authorization}");
+ }
+
public AuthorizeException(String message) {
this(message, 401);
}
diff --git a/hsweb-examples/hsweb-examples-simple/pom.xml b/hsweb-examples/hsweb-examples-simple/pom.xml
index 71ddd0d3c..cb7a9093d 100644
--- a/hsweb-examples/hsweb-examples-simple/pom.xml
+++ b/hsweb-examples/hsweb-examples-simple/pom.xml
@@ -85,12 +85,6 @@
org.hswebframework.web
hsweb-spring-boot-starter
${project.version}
-
-
- com.fasterxml.jackson.core
- jackson-databind
-
-
@@ -127,9 +121,5 @@
io.springfox
springfox-swagger-ui
-
- com.fasterxml.jackson.core
- jackson-databind
-
\ No newline at end of file
diff --git a/hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/TestController.java b/hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/TestController.java
index 3646f5f3d..445571446 100644
--- a/hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/TestController.java
+++ b/hsweb-examples/hsweb-examples-simple/src/main/java/org/hswebframework/web/example/simple/TestController.java
@@ -1,11 +1,13 @@
package org.hswebframework.web.example.simple;
import io.swagger.annotations.ApiOperation;
+import org.hswebframework.web.AuthorizeException;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.authorization.AuthenticationHolder;
import org.hswebframework.web.authorization.Permission;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.RequiresDataAccess;
+import org.hswebframework.web.authorization.annotation.RequiresExpression;
import org.hswebframework.web.authorization.annotation.RequiresFieldAccess;
import org.hswebframework.web.commons.entity.Entity;
import org.hswebframework.web.commons.entity.PagerResult;
@@ -27,8 +29,8 @@ import java.util.List;
* @author zhouhao
*/
@RestController
-@Authorize(permission = "test")
@RequestMapping("/test")
+@Authorize(permission = "test")
public class TestController implements QueryController {
@@ -80,8 +82,7 @@ public class TestController implements QueryController updateLoginUserPassword(@RequestParam String password,
@RequestParam String oldPassword) {
- Authentication authentication = Authentication.current().get();
+
+ Authentication authentication = Authentication.current().orElseThrow(AuthorizeException::new);
getService().updatePassword(authentication.getUser().getId(), oldPassword, password);
return ok();
}