当前位置:网站首页>Platform management background and merchant menu resource management: access control design of platform management background
Platform management background and merchant menu resource management: access control design of platform management background
2022-06-26 16:58:00 【Seconds to write code】
Platform management background access control design
The access control design here uses Spring Secutiry To achieve , These contents are similar to those in the second chapter 10 Chapter SSO The implementation methods of the access control part in the design are similar , The difference is that there's no need for OAuth 2, And the design of authority management also uses a more simple method to achieve . Let's skip some of the same things , Explain the differences only . These designs are all in modules manage-web Implemented in .

Using operators in access control
Create a MyUserDetails class , Realization Spring Secutiry Of UserDetails, To import Operators Users and their rights management , The code is as follows :
public class MyUserDetails implements UserDetails {
private String username;
private String password;
private Collection<? extends GrantedAuthority> authorities;
private Operators operators;
public MyUserDetails(String username,String password, collection<? extendsGrantedAuthority>authorities,Operators operators) {
this.username = username;
this.password = password;
this.authorities =authorities;this.operators = operators;
this.operators.setPassword(null);
)
...
}Create a MyUserDetailsService Service , And in the configuration class SecurityConfiguration Quote from . So that we can Spring Secutiry Using our defined users and their permissions for secure access control Authentication . Specific implementation details can refer to the previous article SSO Design .

Platform management background authority management design
Here the authority management uses a relatively simple method to achieve , That is to use configuration parameters to realize authority management , The implementation method is as follows .
First , Add the following configuration items to the application configuration of the module :
securityconfig:
logoutsuccssurl:/
permitall:-/druid/**- /bbs**
deniedpage:/deny
urlroles:/**/new/** =admins;
/**/edit/**=admins, editors;/**/delete/** =adminsThese configuration parameters are defined by a custom configuration class SecuritySettings Realization .
among ,urlroles Configuration parameters for rights management . This configuration parameter is passed through the requested URL Set the user's access rights . There are only two roles set up here ( Or user groups ) Authority , They are admins and editors. stay URL In resource allocation , Combining wildcards “*”, Use keywords separately new、edit and delete Means new 、 Edit and delete operations .
In the design of the controller , You also need to use these keyword settings URL, For example, some of the following @RequestMapping Design :
@RequestMapping( "/new")
CRequestMapping(value="/edit/{id] ")
@RequestMapping(value="/update",method = RequestMethod.POST)@RequestMapping(value=" /delete/{id} ") secondly , Metadata management in security resource management
CustomSecurityMetadataSource in , Use the design shown below :
public CustomsecurityMetadataSource (String urlroles){
super();
this.urlroles =urlroles;
resourceMap = loadResourceMatchAuthority();
private Map<String, Collection<ConfigAttribute>>loadResourceMatchAuthority({
Map<String, Collection<ConfigAttribute>>map = new HashMap<String,
Collection<ConfigAttribute>>(0);
if (urlroles !=null && !urlroles.isEmpty()){
String[] resouces = urlroles.split(";");for(String resource : resouces){
String[]urls = resource.split("=");String[roles = urls[1].split(",");Collection<ConfigAttribute> list = new
ArrayList<ConfigAttribute>();
for(String role :roles){
ConfigAttribute config =new SecurityConfig (role.trim());list.add (config);
//key: url,value: roles
map.put(urls[0].trim(), list);
}else{
logger.error("'securityconfig.urlroles' must be set");
}
logger.info ( "Loaded UrlRoles Resources. ");return map;
}This design means , When the system starts , Import the data of the above permission configuration as the metadata of security management , To provide the basis for the later authority check .
Last , In authority check
CustomAccessDecisionManager In the design of , Use the design shown below :
public class CustomAccessDecisionManager implements AccessDecisionManager
{
protected Log log = LogFactory.getLog (getClass());
@override
public void decide (Authentication authentication, 0bject object,
Collection<configAttribute>configAttributes)
throws AccessDeniedException,InsufficientAuthenticationException tif(configAttributes == null) {
return;
}
//config urlroles
Iterator<ConfigAttribute>iterator = configAttributes.iterator();
while (iterator.hasNext()){
ConfigAttribute configAttribute = iterator.next();//need role
String needRole = configAttribute.getAttribute();//user roles
for (GrantedAuthority ga : authentication.getAuthorities())I
if (needRole.equals(ga.getAuthority())) {
return;
}
}
log.info("need role is " + needRole);
}
throw new AccessDeniedException ( "Cannot Access!");
}
}When a user accesses a resource that contains metadata for security management , Check whether there is a matching role in the user's role list , In order to achieve the purpose of authority verification .
This simplified design requires that when we create characters , The name must match the name in the configuration , That is, use the admins and editors.
If you want to control permissions through data management , Realize more abundant authority management function , You can refer to 10.4 The content of section .
After completing all the above designs , You can start testing .
Direct start manage-web application , After successful startup , Enter the following link in the browser to log in to the system :
http://localhost:8099Use the user name generated in the previous unit test admin Log in to the system . After logging into the system, you can manage the data of operators and their roles , Pictured 11-1 Shown .

The content of this article is platform management background and business menu resource management : Platform management background access control design
- The next article is to explain the platform management background and business menu resource management : Business registration management design ;
- Friends who think the article is good can forward this article and pay attention to Xiaobian ;
- Thank you for your support !
边栏推荐
- Call the random function to generate 20 different integers and put them in the index group of institute a
- Environment setup mongodb
- Calculate the average of N numbers in the index group of X, and return the number that is less than the average and closest to the average through formal parameters
- Teach you to learn dapr - 9 Observability
- 构造函数和析构函数
- Overall context of concurrent programming
- 当一个程序员一天被打扰 10 次,后果很惊人!
- 数字藏品与NFT到底有何区别
- 防火 疏散 自救…这场安全生产暨消防培训干货满满!
- 内存分区模型
猜你喜欢

Web3去中心化存储生态图景

5G未平6G再启,中国引领无线通信,6G的最大优势在哪里?

知道这几个命令让你掌握Shell自带工具
Teach you to learn dapr - 6 Publish subscription
![[force deduction question] two point search: 4 Find the median of two positive arrays](/img/4f/43aa7e14344e7e1a2fb7c1d209d13b.png)
[force deduction question] two point search: 4 Find the median of two positive arrays

QT 5.9.8 installation tutorial

Teach you to learn dapr - 5 Status management
![[suggested collection] 11 online communities suitable for programmers](/img/6b/d5c68e93384fd314d0cb27d9df1cb9.jpg)
[suggested collection] 11 online communities suitable for programmers

Implementation of MySQL master-slave architecture

Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)
随机推荐
How can I get the stock account opening discount link? Is online account opening safe?
Teach you to learn dapr - 2 Must know concept
C语言所有知识点小结
Codeforces Round #802 (Div. 2)
Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
Teach you to learn dapr - 6 Publish subscription
Overall context of concurrent programming
[from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)
Pybullet robot simulation environment construction 5 Robot pose visualization
无需人工先验!港大&同济&LunarAI&旷视提出基于语义分组的自监督视觉表征学习,显著提升目标检测、实例分割和语义分割任务!...
Cloud platform monitoring system based on stm32+ Huawei cloud IOT design
Use the array to calculate the average of N numbers, and output the numbers greater than the average
Some instance methods of mono
Discussion: the next generation of stable coins
[graduation season] a word for graduates: the sky is high enough for birds to fly, and the sea is wide enough for fish to leap
Stm32f103c8t6 realize breathing lamp code
Introduction to minimal API
[suggested collection] 11 online communities suitable for programmers
[matlab project practice] prediction of remaining service life of lithium ion battery based on convolutional neural network and bidirectional long short time (cnn-lstm) fusion
Summary of all knowledge points of C language