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.
- Loading branch information
Showing
17 changed files
with
460 additions
and
41 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
10 changes: 10 additions & 0 deletions
10
...ization-api/src/main/java/org/hswebframework/web/authorization/AuthenticationRequest.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,10 @@ | ||
package org.hswebframework.web.authorization; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* @author zhouhao | ||
* @since 3.0.0-RC | ||
*/ | ||
public interface AuthenticationRequest extends Serializable { | ||
} |
21 changes: 21 additions & 0 deletions
21
...webframework/web/authorization/simple/PlainTextUsernamePasswordAuthenticationRequest.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,21 @@ | ||
package org.hswebframework.web.authorization.simple; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.hswebframework.web.authorization.AuthenticationRequest; | ||
|
||
/** | ||
* @author zhouhao | ||
* @since 3.0.0-RC | ||
*/ | ||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class PlainTextUsernamePasswordAuthenticationRequest implements AuthenticationRequest { | ||
private String username; | ||
|
||
private String password; | ||
} |
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
88 changes: 88 additions & 0 deletions
88
...ain/java/org/hswebframework/web/authorization/basic/embed/EmbedAuthenticationManager.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,88 @@ | ||
package org.hswebframework.web.authorization.basic.embed; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hswebframework.web.authorization.Authentication; | ||
import org.hswebframework.web.authorization.AuthenticationManager; | ||
import org.hswebframework.web.authorization.AuthenticationRequest; | ||
import org.hswebframework.web.authorization.builder.DataAccessConfigBuilderFactory; | ||
import org.hswebframework.web.authorization.simple.PlainTextUsernamePasswordAuthenticationRequest; | ||
import org.hswebframework.web.authorization.simple.builder.SimpleDataAccessConfigBuilderFactory; | ||
import org.hswebframework.web.validate.ValidationException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.util.StringUtils; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author zhouhao | ||
* @since 3.0.0-RC | ||
*/ | ||
@ConfigurationProperties(prefix = "hsweb") | ||
public class EmbedAuthenticationManager implements AuthenticationManager { | ||
|
||
private Map<String, Authentication> authentications = new HashMap<>(); | ||
|
||
@Autowired(required = false) | ||
private DataAccessConfigBuilderFactory dataAccessConfigBuilderFactory = new SimpleDataAccessConfigBuilderFactory(); | ||
|
||
@Getter | ||
@Setter | ||
private Map<String, EmbedAuthenticationProperties> users = new HashMap<>(); | ||
|
||
@PostConstruct | ||
public void init() { | ||
users.forEach((id, properties) -> { | ||
if (StringUtils.isEmpty(properties.getId())) { | ||
properties.setId(id); | ||
} | ||
for (EmbedAuthenticationProperties.PermissionInfo permissionInfo : properties.getPermissions()) { | ||
for (Map<String, Object> objectMap : permissionInfo.getDataAccesses()) { | ||
for (Map.Entry<String, Object> stringObjectEntry : objectMap.entrySet()) { | ||
if (stringObjectEntry.getValue() instanceof Map) { | ||
Map mapVal = ((Map) stringObjectEntry.getValue()); | ||
boolean maybeIsList = mapVal.keySet().stream().allMatch(org.hswebframework.utils.StringUtils::isInt); | ||
if (maybeIsList) { | ||
stringObjectEntry.setValue(mapVal.values()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Authentication authenticate(AuthenticationRequest request) { | ||
if (request instanceof PlainTextUsernamePasswordAuthenticationRequest) { | ||
return sync(users.values().stream() | ||
.filter(user -> | ||
((PlainTextUsernamePasswordAuthenticationRequest) request).getUsername().equals(user.getUsername()) | ||
&& ((PlainTextUsernamePasswordAuthenticationRequest) request).getPassword().equals(user.getPassword())) | ||
.findFirst() | ||
.map(properties -> properties.toAuthentication(dataAccessConfigBuilderFactory)) | ||
.orElseThrow(() -> new ValidationException("用户不存在"))); | ||
} | ||
|
||
throw new UnsupportedOperationException("不支持的授权类型:" + request); | ||
|
||
} | ||
|
||
@Override | ||
public Authentication getByUserId(String userId) { | ||
return authentications.get(userId); | ||
} | ||
|
||
@Override | ||
public Authentication sync(Authentication authentication) { | ||
authentications.put(authentication.getUser().getId(), authentication); | ||
return authentication; | ||
} | ||
|
||
void addAuthentication(Authentication authentication) { | ||
sync(authentication); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
.../java/org/hswebframework/web/authorization/basic/embed/EmbedAuthenticationProperties.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,97 @@ | ||
package org.hswebframework.web.authorization.basic.embed; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.hswebframework.web.authorization.Authentication; | ||
import org.hswebframework.web.authorization.Permission; | ||
import org.hswebframework.web.authorization.Role; | ||
import org.hswebframework.web.authorization.builder.DataAccessConfigBuilderFactory; | ||
import org.hswebframework.web.authorization.simple.SimpleAuthentication; | ||
import org.hswebframework.web.authorization.simple.SimplePermission; | ||
import org.hswebframework.web.authorization.simple.SimpleRole; | ||
import org.hswebframework.web.authorization.simple.SimpleUser; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* <pre> | ||
* hsweb: | ||
* users: | ||
* admin: | ||
* name: 超级管理员 | ||
* username: admin | ||
* password: admin | ||
* roles: | ||
* - id: admin | ||
* name: 管理员 | ||
* - id: user | ||
* name: 用户 | ||
* permissions: | ||
* - id: user-manager | ||
* actions: * | ||
* dataAccesses: | ||
* - action: query | ||
* type: DENY_FIELDS | ||
* fields: password,salt | ||
* </pre> | ||
* | ||
* @author zhouhao | ||
* @since 3.0.0-RC | ||
*/ | ||
@Getter | ||
@Setter | ||
public class EmbedAuthenticationProperties { | ||
|
||
private String id; | ||
|
||
private String name; | ||
|
||
private String username; | ||
|
||
private String type; | ||
|
||
private String password; | ||
|
||
private List<SimpleRole> roles = new ArrayList<>(); | ||
|
||
private List<PermissionInfo> permissions = new ArrayList<>(); | ||
|
||
@Getter | ||
@Setter | ||
public static class PermissionInfo { | ||
private String id; | ||
|
||
private Set<String> actions = new HashSet<>(); | ||
|
||
private List<Map<String, Object>> dataAccesses = new ArrayList<>(); | ||
} | ||
|
||
public Authentication toAuthentication(DataAccessConfigBuilderFactory factory) { | ||
SimpleAuthentication authentication = new SimpleAuthentication(); | ||
SimpleUser user = new SimpleUser(); | ||
user.setId(id); | ||
user.setName(name); | ||
user.setUsername(username); | ||
user.setType(type); | ||
authentication.setUser(user); | ||
authentication.setRoles((List) roles); | ||
List<Permission> permissionList = permissions.stream() | ||
.map(info -> { | ||
SimplePermission permission = new SimplePermission(); | ||
permission.setId(info.getId()); | ||
permission.setActions(info.getActions()); | ||
permission.setDataAccesses(info.getDataAccesses() | ||
.stream().map(conf -> factory.create() | ||
.fromJson(JSON.toJSONString(conf)) | ||
.build()).collect(Collectors.toSet())); | ||
return permission; | ||
|
||
}).collect(Collectors.toList()); | ||
|
||
authentication.setPermissions(permissionList); | ||
return authentication; | ||
} | ||
|
||
} |
Oops, something went wrong.