相关疑难解决方法(0)

如何在Spring Boot中添加过滤器类?

我想知道,如果Filter在Spring Boot中有一个类(用于Web应用程序)的注释?也许@Filter吧?

我想在我的项目中添加自定义过滤器.

Spring Boot Reference Guide提到了 FilterRegistrationBean,但我不知道如何使用它.

java configuration servlet-filters spring-boot

208
推荐指数
12
解决办法
24万
查看次数

Spring RequestMapping用于生成和使用JSON的控制器

有了多个消耗和生成的Spring控制器application/json,我的代码充满了长注释,如:

    @RequestMapping(value = "/foo", method = RequestMethod.POST,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
Run Code Online (Sandbox Code Playgroud)

有没有办法生成一个"复合/继承/聚合"注释,其默认值为consumesproduces,这样我就可以编写如下内容:

    @JSONRequestMapping(value = "/foo", method = RequestMethod.POST)
Run Code Online (Sandbox Code Playgroud)

我们如何定义@JSONRequestMapping上面的内容?注意valuemethod传入就像@RequestMapping,也可以传入consumesproduces默认不适合.

我需要控制我要回来的东西.我想要produces/ consumesannotation-methods,以便获得适当的Content-Type头文件.

java spring json spring-mvc

36
推荐指数
4
解决办法
7万
查看次数

春天的mvc.不区分大小写的get参数映射

根据这个答案我尝试编写我的代码:

POJO:

class MyBean{

    public String getValueName() {
        return valueName;
    }

    public void setValueName(String valueName) {
        this.valueName = valueName;
    }

    String valueName;
}
Run Code Online (Sandbox Code Playgroud)

内部控制器:

    @ModelAttribute
    public MyBean createMyBean() {
        return new MyBean();
    }
    @RequestMapping(value = "/getMyBean", method = RequestMethod.GET)
    public String getMyBean(@ModelAttribute MyBean myBean) {
        System.out.println(myBean.getValueName());
        return "pathToJsp";
    }
Run Code Online (Sandbox Code Playgroud)

web.xml配置:

<filter>
    <filter-name>caseInsensitiveFilter</filter-name>
    <filter-class>com.terminal.interceptor.CaseInsensitiveRequestFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>caseInsensitiveFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)

过滤:

@Component
public class CaseInsensitiveRequestFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException …
Run Code Online (Sandbox Code Playgroud)

java mapping spring spring-mvc servlet-filters

10
推荐指数
1
解决办法
3828
查看次数

/ api-url在Spring Boot Security中有一个空的过滤器列表

具有REST服务的Spring Boot应用程序必须允许公共访问某些服务,同时将其他服务限制为仅授权用户.当configure(WebSecurity web)方法添加到SecurityConfig类中时,如下所示,a将403 error被发送到用户的Web浏览器,并且Spring Boot日志文件会发出错误消息,指出:

/registration-form has an empty filter list  
Run Code Online (Sandbox Code Playgroud)

需要对下面的代码进行哪些具体更改才能将/registration-form服务成功提供给任何用户,包括匿名/未经身份验证的用户?

这是SecurityConfig班级:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity webSecurity) throws Exception {
        webSecurity.ignoring().antMatchers("/registration-form");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .formLogin()
                .and()
            .httpBasic().and()
            .authorizeRequests()
                .antMatchers("/login1").permitAll()
                .antMatchers("/login2").permitAll()
                .anyRequest().authenticated();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是完整的日志:

2016-04-07 16:42:18.548  INFO 8937 --- [nio-8001-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2016-04-07 16:42:18.548  INFO …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot

10
推荐指数
1
解决办法
6031
查看次数

在Spring Boot MVC中添加ShallowEtagHeaderFilter

我正在尝试调整我的应用程序配置以设置ETag支持.

我刚刚检查了这个问题,所以让我说一下我的代码与它的不同之处:

  1. 我不使用任何xml配置文件.
  2. 我正在为系统的每个方面使用不同的配置类.我WebConfig看起来像这样:

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = { "xxx", "yyy" })
public class WebConfig extends WebMvcConfigurerAdapter {

   @Bean
   public Filter shallowETagHeaderFilter() {
        return new ShallowEtagHeaderFilter();
   }
      ...
}

  1. 我的SecurityConfig看起来像这样:

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

        @Override
        protected void configure(final HttpSecurity http) throws Exception {
            http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and().exceptionHandling()
                        .authenticationEntryPoint(authenticationEntryPoint())
                .and().authorizeRequests()
                        .antMatchers(HttpMethod.GET, "/**").authenticated()
                        .antMatchers(HttpMethod.POST, "/**").authenticated()
                        .antMatchers(HttpMethod.HEAD, "/**").authenticated()
            .and().csrf().disable()    
            .addFilterBefore(authenticationTokenProcessingFilter(), UsernamePasswordAuthenticationFilter.class);
        }

    }

  1. 我还有一个初始化类,它是空的:

    @Order(value=1)
    public class SecurityWebAppInitializer extends AbstractSecurityWebApplicationInitializer {

    }

我没有看到任何地方ShallowEtagHeaderFilter被添加到默认链或任何东西,我如何在此设置中使用它?

etag spring annotations servlet-filters spring-boot

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