forked from hs-web/hsweb-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request hs-web#90 from hs-web/3.0.x
3.0.1
- Loading branch information
Showing
166 changed files
with
706 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...ion-api/src/main/java/org/hswebframework/web/authorization/define/AuthorizingContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...n/java/org/hswebframework/web/authorization/basic/handler/UserAllowPermissionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.hswebframework.web.authorization.basic.handler; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hswebframework.web.authorization.define.AuthorizingContext; | ||
import org.hswebframework.web.authorization.define.HandleType; | ||
import org.hswebframework.web.authorization.listener.event.AuthorizingHandleBeforeEvent; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.util.AntPathMatcher; | ||
import org.springframework.util.ClassUtils; | ||
import org.springframework.util.PathMatcher; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* <pre> | ||
* hsweb: | ||
* authorize: | ||
* allows: | ||
* users: | ||
* admin: * | ||
* guest: **.query* | ||
* roles: | ||
* admin: * | ||
* | ||
* </pre> | ||
* | ||
* @author zhouhao | ||
* @since 3.0.1 | ||
*/ | ||
@ConfigurationProperties("hsweb.authorize") | ||
public class UserAllowPermissionHandler { | ||
|
||
@Getter | ||
@Setter | ||
private Map<String, Map<String, String>> allows = new HashMap<>(); | ||
|
||
private PathMatcher pathMatcher = new AntPathMatcher("."); | ||
|
||
@EventListener | ||
public void handEvent(AuthorizingHandleBeforeEvent event) { | ||
|
||
if (allows.isEmpty() || event.getHandleType() == HandleType.DATA) { | ||
return; | ||
} | ||
AuthorizingContext context = event.getContext(); | ||
|
||
// class full name.method | ||
String path = ClassUtils.getUserClass(context.getParamContext() | ||
.getTarget()) | ||
.getName().concat(".") | ||
.concat(context.getParamContext() | ||
.getMethod().getName()); | ||
|
||
String userId = context.getAuthentication().getUser().getId(); | ||
boolean allow; | ||
allow = Optional.ofNullable(allows.get("users")) | ||
.map(users -> users.get(userId)) | ||
.filter(pattern -> "*".equals(pattern) || pathMatcher.match(pattern, path)) | ||
.isPresent(); | ||
if (allow) { | ||
event.setAllow(true); | ||
return; | ||
} | ||
allow = context.getAuthentication() | ||
.getRoles() | ||
.stream() | ||
.map(role -> allows.getOrDefault("roles", Collections.emptyMap()).get(role.getId())) | ||
.filter(Objects::nonNull) | ||
.anyMatch(pattern -> "*".equals(pattern) || pathMatcher.match(pattern, path)); | ||
if (allow) { | ||
event.setAllow(true); | ||
return; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...ic/src/test/groovy/org/hswebframework/web/authorization/basic/handler/TestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.hswebframework.web.authorization.basic.handler; | ||
|
||
import org.hswebframework.web.authorization.annotation.Authorize; | ||
import org.hswebframework.web.controller.message.ResponseMessage; | ||
|
||
/** | ||
* @author zhouhao | ||
* @since 3.0.1 | ||
*/ | ||
public class TestController { | ||
|
||
public ResponseMessage<String> query() { | ||
return ResponseMessage.ok(); | ||
} | ||
|
||
public ResponseMessage<String> update() { | ||
return ResponseMessage.ok(); | ||
} | ||
|
||
public ResponseMessage<String> delete() { | ||
return ResponseMessage.ok(); | ||
} | ||
|
||
} |
Oops, something went wrong.