package com.ycl.config; import com.ycl.component.DynamicSecurityService; import com.ycl.entity.user.UmsMenu; import com.ycl.entity.user.UmsResource; import com.ycl.service.user.UmsAdminService; import com.ycl.service.user.UmsMenuService; import com.ycl.service.user.UmsResourceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.access.ConfigAttribute; import org.springframework.security.core.userdetails.UserDetailsService; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * security模块相关配置 * 自定义配置,用于配置如何获取用户信息及动态权限 */ @Configuration public class YclSecurityConfig { @Autowired private UmsAdminService adminService; @Autowired private UmsMenuService umsMenuService; @Bean public UserDetailsService userDetailsService() { //获取登录用户信息 return username -> adminService.loadUserByUsername(username); } @Bean public DynamicSecurityService dynamicSecurityService() { return new DynamicSecurityService() { @Override public Map loadDataSource() { Map map = new ConcurrentHashMap<>(); List resourceList = umsMenuService.list(); for (UmsMenu resource : resourceList) { map.put(resource.getTitle(), new org.springframework.security.access.SecurityConfig(resource.getId() + ":" + resource.getTitle())); } return map; } }; } }