我在哪里可以找到学习Ant路径样式约定的资源?我已经去了Ant站点,但找不到路径样式的任何信息.
是否可以为某种HTTP方法禁用Spring Security?
我们有一个Spring REST应用程序,其服务要求将授权令牌附加到http请求的标头中.我正在为它编写一个JS客户端,并使用JQuery发送GET/POST请求.该应用程序使用此过滤器代码启用CORS.
doFilter(....) {
HttpServletResponse httpResp = (HttpServletResponse) response;
httpResp.setHeader("Access-Control-Allow-Origin", "*");
httpResp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
httpResp.setHeader("Access-Control-Max-Age", "3600");
Enumeration<String> headersEnum = ((HttpServletRequest) request).getHeaders("Access-Control-Request-Headers");
StringBuilder headers = new StringBuilder();
String delim = "";
while (headersEnum.hasMoreElements()) {
headers.append(delim).append(headersEnum.nextElement());
delim = ", ";
}
httpResp.setHeader("Access-Control-Allow-Headers", headers.toString());
}
Run Code Online (Sandbox Code Playgroud)
但是当JQuery发送对CORS的OPTIONS请求时,服务器会使用Authorization Failed令牌进行响应.显然OPTIONS请求缺少授权令牌.那么可以让OPTIONS从Spring安全配置中逃脱安全层吗?
当我注意到/*和/**模式之间存在差异时,我试图为过滤器注册某些URL .
@Bean
public FilterRegistrationBean tokenAuthenticationFilterBean() {
FilterRegistrationBean registration = new FilterRegistrationBean(tokenAuthenticationFilter);
registration.addUrlPatterns("/api/**","/ping","/api/*");
return registration;
}
Run Code Online (Sandbox Code Playgroud)
这些模式有什么区别?
当我尝试PutMapping在Postman中使用我的API 时,我收到“无效的CORS请求” 。但这对于“ POST”和“ GET”映射工作正常。
为什么对“ PUT”操作不起作用?
我的Spring Boot版本:2.0
这是我的配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/h2-console/**/**").permitAll()
.antMatchers(HttpMethod.GET,"/user/get-request").permitAll()
.antMatchers(HttpMethod.POST,"/user/post-request").permitAll()
.antMatchers(HttpMethod.PUT,"/user/put-request").permitAll()
.and()
.exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint)
.and()
.addFilter(new JwtAuthenticationFilter(authenticationManager()))
.addFilter(new JwtAuthorizationFilter(authenticationManager(), jwtUserDetailService));
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedHeaders("*").exposedHeaders("Authorization");
}
};
}
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
@RestController
@RequestMapping("/user")
public class UserController {
@PutMapping("/put-request")
public void doResetPassword(@RequestBody String password) {
System.out.println("PUT MAPPING");
}
@PostMapping("/post-request")
public void doResetPassword(@RequestBody String password) { …Run Code Online (Sandbox Code Playgroud) 我正在开发一个简单的网络应用程序Spring boot 1.5.3,我需要所有路由来发送静态index.html文件。现在,我有这个:
@Controller
public class IndexController {
@RequestMapping("/*")
public String index(final HttpServletRequest request) {
final String url = request.getRequestURI();
if (url.startsWith("/static")) {
return String.format("forward:/%s", url);
}
return "forward:/static/index.html";
}
}
Run Code Online (Sandbox Code Playgroud)
我的应用程序仅包含静态资产和 REST API。但问题是上面显示的控制器只匹配第一级 url /index,如/department等。我想匹配所有 url 级别,如/index/some/other/staff等。我该怎么做?
附言。我尝试使用/**中的模板@RequestMapping,但我的应用程序因错误而中断StackOverflow。
更新
如果在 url 中再添加一级,那么所有内容都会按预期工作:
@Controller
public class IndexController {
@RequestMapping("/test/**")
public String index(final HttpServletRequest request) {
final String url = request.getRequestURI();
if (url.startsWith("/static")) {
return String.format("forward:/%s", …Run Code Online (Sandbox Code Playgroud) 我想在每个 API REST 请求上添加一个 Elapsed-Time 响应标头参数,即使是那些以错误结束的请求。
例如:
===>
GET /api/customer/123 HTTP/1.1
Accept: application/vnd.company.app.customer-v1+json
<===
HTTP/1.1 200 OK
Content-Type: application/vnd.company.app.customer-v1+json
Elapsed-Time: 12
{ id: 123, name: Jordi, age: 28 }
Run Code Online (Sandbox Code Playgroud)
作为 Elapsed-Time 参数计算为@RequestMapping 方法完成的瞬间与@RequestMapping 方法开始的瞬间之间的毫秒差。
我一直在看 Spring4 HandlerInterceptorAdapter。preHandle 和 postHandle 方法似乎非常适合这种情况(尽管执行链中的每个拦截器都有时间开销)。但是,不幸的是,更改 postHandle 方法中的响应标头不起作用,因为响应已经构建。
Spring 文档指出:
请注意,HandlerInterceptor 的 postHandle 方法并不总是非常适合与 @ResponseBody 和 ResponseEntity 方法一起使用。在这种情况下,HttpMessageConverter 在调用 postHandle 之前写入并提交响应,这使得无法更改响应,例如添加标头。相反,应用程序可以实现 ResponseBodyAdvice 并将其声明为 @ControllerAdvice bean 或直接在 RequestMappingHandlerAdapter 上配置它。
您知道处理这种情况的任何有效的优雅解决方案吗?
我不认为这种情况是在复制Spring - 在处理后修改每个请求的标头(在 postHandle 中),因为我需要捕获一个变量,其值为开始时间(当请愿到达应用程序时并且在 @RequestMapping 方法开始之前) ),然后在@RequestMapping 方法完成后使用此变量来计算经过的时间。
java ×4
spring ×4
spring-boot ×3
cors ×2
spring-mvc ×2
ant ×1
conventions ×1
path ×1
put ×1
rest ×1
security ×1