Java 如何使用权限保护RESTAPI

Java 如何使用权限保护RESTAPI,java,spring,rest,security,Java,Spring,Rest,Security,我正在使用spring boot开发rest api服务,为了检查权限,我使用令牌作为-在登录请求中将键值保存到memcache,将其令牌(字符串)的长度设置为50,值设置为您的id。要访问任何rest请求,我的rest控制器将此令牌作为get参数获取(与post、put相同的逻辑),而不是获取用户id(来自memcache)并检查此用户是否具有特定资源的权限。例如,“Carwash/2 PUT”洗车机对公司有FK,用户对公司也有FK,如果这些FK类似,则您有权更新洗车机。我正在寻找另一种方法来

我正在使用spring boot开发rest api服务,为了检查权限,我使用令牌作为-在登录请求中将键值保存到memcache,将其令牌(字符串)的长度设置为50,值设置为您的id。要访问任何rest请求,我的rest控制器将此令牌作为get参数获取(与post、put相同的逻辑),而不是获取用户id(来自memcache)并检查此用户是否具有特定资源的权限。例如,“Carwash/2 PUT”洗车机对公司有FK,用户对公司也有FK,如果这些FK类似,则您有权更新洗车机。我正在寻找另一种方法来使用spring security保护我的rest api,因为当前方法不安全(我在url中看到令牌,并在所有HTTP请求中使用get参数)。但在官方文档中给出的示例中,spring安全性似乎只适用于权限枚举和登录页面

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
      auth.inMemoryAuthentication().withUser("anon").password("123456").roles("USER");
      auth.inMemoryAuthentication().withUser("admin").password("123456").roles("ADMIN");
      auth.inMemoryAuthentication().withUser("userr").password("123456").roles("DBA");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

      http.authorizeRequests()
        .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
        .antMatchers("/dba/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_DBA')")
        .and().formLogin();

    }
}

如何使用此方法检查特定洗车许可?

您可以使用spring security中的常见内置表达式,如

http.authorizeRequests().hasAuthority("ROLE_ADMIN").antMatchers("/dump/**")
或者可以将方法安全表达式与注释一起使用,如

@PreAuthorize("#id == user.companyId") 
你也可以看看那里

我认为您需要Spring acl: