小编Aet*_*ros的帖子

MongoTemplate Criteria Query

我正在Mongo根据多个参数生成复杂的查询.我想用Criteriahelper类制作的标准之一是:

{"field1": {$exists: true, $ne: false}}
Run Code Online (Sandbox Code Playgroud)

我试着用它来做:

Criteria.where("field1").is(Criteria.where("$ne").is(false).and("$exists").is(true))
Run Code Online (Sandbox Code Playgroud)

但它会产生:

{ "field1" : { $java : org.springframework.data.mongodb.core.query.Criteria@23864e60 } 
Run Code Online (Sandbox Code Playgroud)

那么,如何实现我需要的确切查询? 我无法对该查询字符串进行硬编码,因为这些类型标准是为field1,... fieldN动态生成的,然后与$or:

statusCriteria = statusCriteria.orOperator(criterias.toArray(new Criteria[criterias.size()]));
Run Code Online (Sandbox Code Playgroud)

java mongodb spring-data-mongodb mongotemplate

6
推荐指数
1
解决办法
2万
查看次数

使用@Secured注释保护MVC控制器方法

这是我使用SpringBoot制作的API的单一安全配置:

@Configuration
@EnableWebSecurity
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenAuthenticationService tokenAuthenticationService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/static/**").permitAll()
                .antMatchers(HttpMethod.POST, "/api/user/registration").permitAll()
                .antMatchers(HttpMethod.POST, "/api/user/authentication").permitAll()
                .anyRequest().authenticated().and()
            .addFilterBefore(new TokenLoginFilter("/api/user/authentication", authenticationManagerBean(), tokenAuthenticationService), 
                    UsernamePasswordAuthenticationFilter.class)
            .addFilterBefore(new TokenAuthenticationFilter(tokenAuthenticationService),UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
    }

}
Run Code Online (Sandbox Code Playgroud)

所有URL都映射到spring web mvc控制器,我想手动指定控制器及其方法的访问级别,如下所示:

@RestController
@RequestMapping("/api/resource/")
@Secured("ROLE_ANONYMOUS") //as default role
public class ResourceController {
    ...

    @Secured("ROLE_USER")
    some_method...
}
Run Code Online (Sandbox Code Playgroud)

但是当我以匿名用户身份执行/ api/resource/*请求时,应用程序会响应403状态代码,但我希望方法调用.看起来@Secured注释对授权没有影响,并且所有控制器方法仅允许ROLE_USER.

TokenAuthenticationFilter仅在令牌存在时执行操作,因此我猜它没有任何效果.

    @Override
public …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-security

5
推荐指数
1
解决办法
2192
查看次数

从jar运行特定的JMH基准测试

我有几个使用@Benchmark注释的重型基准类.在使用基准测试建立jar后,我可以使用以下命令运行所有这些

java -Xmx4G -jar benchmarks.jar -f 1 -wi 3 -i 10
Run Code Online (Sandbox Code Playgroud)

如果我不想运行所有这些基准测试,如何指定运行基准?

java jmh

4
推荐指数
2
解决办法
2142
查看次数