我想知道,如果Filter在Spring Boot中有一个类(用于Web应用程序)的注释?也许@Filter吧?
我想在我的项目中添加自定义过滤器.
Spring Boot Reference Guide提到了
FilterRegistrationBean,但我不知道如何使用它.
有了多个消耗和生成的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)
有没有办法生成一个"复合/继承/聚合"注释,其默认值为consumes和produces,这样我就可以编写如下内容:
@JSONRequestMapping(value = "/foo", method = RequestMethod.POST)
Run Code Online (Sandbox Code Playgroud)
我们如何定义@JSONRequestMapping上面的内容?注意value并method传入就像@RequestMapping,也可以传入consumes或produces默认不适合.
我需要控制我要回来的东西.我想要produces/ consumesannotation-methods,以便获得适当的Content-Type头文件.
根据这个答案我尝试编写我的代码:
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) 具有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) 我正在尝试调整我的应用程序配置以设置ETag支持.
我刚刚检查了这个问题,所以让我说一下我的代码与它的不同之处:
WebConfig看起来像这样:
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = { "xxx", "yyy" })
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public Filter shallowETagHeaderFilter() {
return new ShallowEtagHeaderFilter();
}
...
}
@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);
}
}
@Order(value=1)
public class SecurityWebAppInitializer extends AbstractSecurityWebApplicationInitializer {
}
我没有看到任何地方ShallowEtagHeaderFilter被添加到默认链或任何东西,我如何在此设置中使用它?