我想在我的Spring mvc项目之一中实现基于URL的授权。在我的Spring mvc项目中,我使用java配置。我已将此站点https://www.baeldung.com/role-and-privilege-for-spring-security-registration以实现基于角色和特权的授权。
所以我创建了下面的表来实现。
这是用户表。
这个角色表。
这是角色和特权之间的映射表。
这是权限表。
以下是控制器端的代码。
@Controller
@RequestMapping(value={"/user"})
public class UserController {
@ResponseBody
@RequestMapping(value={"/userDashboard"},method = RequestMethod.GET)
public String userDashboard(ModelMap model){
return "UserDashboard";
}
@ResponseBody
@RequestMapping(value={"/testUser"},method = RequestMethod.GET)
public String testUser(ModelMap model){
return "TestUser";
}
}
下面是实现UserDetailService的代码。
@Service("authService")
@Transactional
public class AuthService implements UserDetailsService {
@Autowired
private UserDaoInterface userDaoInterface;
private static final Logger log = Logger.getLogger(AuthService.class);
@Override
public UserDetails loadUserByUsername(String userName) {
User user = null;
try {
user = userDaoInterface.getUserByUserName(userName);
} catch (Exception e) {
log.error("AuthService @loadUserByUsername --Exception while fetching user from username",e);
}
if(user == null) {
throw new UsernameNotFoundException("Username not found");
}
UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.getUsername(),
user.getPassword(),!user.getIsDeleted(),true,true,true,getAuthorities(user.getRole()));
log.info("UserService userDetails " + userDetails);
return userDetails;
}
private Collection<? extends GrantedAuthority> getAuthorities(Role role) {
return getGrantedAuthorities(getPrivileges(role));
}
private List<String> getPrivileges(Role role) {
List<String> privileges = new ArrayList<>();
List<Privilege> collection = new ArrayList<>();
collection.addAll(role.getPrivileges());
for (Privilege item : collection) {
privileges.add(item.getName());
}
return privileges;
}
private List<GrantedAuthority> getGrantedAuthorities(List<String> privileges) {
List<GrantedAuthority> authorities = new ArrayList<>();
for (String privilege : privileges) {
authorities.add(new SimpleGrantedAuthority(privilege));
}
return authorities;
}
}
Spring Security配置
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled=true)
@ComponentScan("com.project")
public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService authService;
@Autowired
PersistentTokenRepository tokenRepository;
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(authService);
auth.authenticationProvider(authenticationProvider());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService(authService);
authenticationProvider.setPasswordEncoder(passwordEncoder());
return authenticationProvider;
}
@Bean
public PersistentTokenBasedRememberMeServices getPersistentTokenBasedRememberMeServices() {
return new PersistentTokenBasedRememberMeServices("remember-me", authService, tokenRepository);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated()
.and()
.formLogin()
.loginProcessingUrl("/login")
.defaultSuccessUrl("/login")
.and()
.logout().logoutUrl("/logout")
.logoutSuccessUrl("/")
.and().csrf()
.and().rememberMe().rememberMeParameter("remember-me").tokenRepository(tokenRepository).tokenValiditySeconds(86400);
}
}
当我通过存储在用户表中的凭据登录系统并尝试访问url时,如http://localhost:8080/RestProject/user/testUser然后,用户有权访问此url,但根据上述实现,不应允许用户访问此url。所以我无法理解动态权限在这里是如何工作的?
要配置url授权,您需要更改行http。authorizeRequests()。anyRequest()。已验证()
到
http.authorize的请求(). antMatcher("有些人"). hasAuthory("ROLE_SOMEROLE"). antMatcher("/**").hasAuthory("ROLE_ADMIN").身份验证()
然而,似乎您想要的是在运行时动态更改这些内容。我不确定spring是否让你这么做。但是,您可以通过对角色结构的设计进行一些更改来实现类似的行为。
您需要在权限中添加分组层,并将这些组设置为您的用户角色(这些角色将分配给用户)。然后,您授予的权限将是分配给您的角色/分组的权限。通过这种方式,您可以动态更改这些组下的权限,而无需更改代码。
角色权限结构示例(伪java代码):
注:角色和组可以互换
@Entity
public class Permission {
private String name;
private String description;
... // getters and setters
}
@Entity
public class Role {
private String name;
@ManyToOne
private List<Permission> permissions;
... // getters and setters
}
首先,你需要有一个url来做一些事情。假设它创建了一个新的员工记录。因此,您需要创建一个新的“createEmployee”权限,并将其设置为访问该url所需的权限。这里的想法是创建特定于其所保护的操作的权限。这样以后就不必更改权限了。
http.authorize请求(). antMatcher("thom-url"). hasAuthory("create雇员"). antMatcher("/**").hasAuthory("admin").身份验证()
现在您需要做的就是将“create雇员”权限分配给其职责包括此操作的角色。例如,它将是“雇佣经理”角色/分组。
将权限分配给角色/分组的Ex:
Role hiringManager = new Role("hiringManager");
Permission createPermission = permissionRepository.findByName("createPermission");
hiringManager.getPermissions().add(createPermission);
这只是为了举例说明。理想情况下,您的应用程序中会有一个屏幕,允许您在运行时创建组和权限。然后,您可以根据需要从组中分配或删除权限。
请注意,这只是技术方面的一个解决方案。也就是说,即使它不符合您的需求,它也应该引导您提出您的业务需求。
希望这有帮助!
基于路径的授权 Apache和svnserve都可以给用户赋予(或拒绝)访问许可,通常是对整个版本库:一个用户可以读版本库(或不),而且他可以写版本库(或不)。如果可能,也可以定义细粒度的访问规则。一组用户可以有版本库的一个目录的读写权限,但是没有其它的;另一个目录可以是只对一少部分用户可读。 两种服务器都使用同样的文件格式描述路径为基础的规则,如果是Apache,需要加载mod_authz_sv
我对Spring靴还不熟悉。我需要在spring boot中实现基于角色的授权。我有不同的角色,多个用户将映射到每个角色。每当调用api时,我都会设置不同的访问权限(读取、添加、删除、编辑),需要检查访问权限并允许权限。我计划使用拦截器调用具有查询的方法,以从DB获取访问权限,并拒绝或访问api。有没有其他更好的方法我可以用同样的方法?
我是oAuth2安全系统的新手。关于访问REST资源的基于用户角色的授权,我有一个问题。我的互联网冲浪提供了关于oauth2的身份验证部分的输入。 让我提供给你困扰我的情况。
我真的很难找到一个AWS设计,让我: 使用REST(不使用AWS sdk库)向cognito验证实体。 cognito的RESTAPI需要识别实体并根据实体返回结果。例如,像“getOrders”这样的api将返回与已登录的实体关联的订单 经评估的解决方案: > 具有作用域的oAuth身份验证无法解决此场景。实体必须使用appclientId和secret进行身份验证,因此不清楚如何区分实体(为所
我的web应用程序正在使用spring security进行基于数据库的身份验证和授权。我的配置如下:这是我的web.xml文件,我引入了applicationcontext.xml和applicationcontext-security.xml: 这是我的ApplicationContextSecurity.xml:
本文向大家介绍基于Docker的MongoDB实现授权访问的方法,包括了基于Docker的MongoDB实现授权访问的方法的使用技巧和注意事项,需要的朋友参考一下 基于Docker部署一个数据库实例通常比直接在服务器上安装数据库还要简单,Gevin在开发环境中经常使用基于docker的数据库服务,docker也渐渐成为Gevin在Linux上安装MongoDB的首选方式,由于MongoDB默认是不