![](/img/trans.png)
[英]How do i Handle a "ñ" character that comes as a "@PathVariable" in a request, Spring Boot
[英]How can I get @PathVariable inside FilterRegistrationBean? Spring Boot
我请求的路径是:
localhost:8080/companies/12/accounts/35
我的 Rest Controller 包含此功能,我想在 Filter 中获取 companyId 和 accountId。
@RequestMapping(value = "/companies/{companyId}/accounts/{accountId}", method = RequestMethod.PUT)
public Response editCompanyAccount(@PathVariable("companyId") long companyId, @PathVariable("accountId") long accountId,
@RequestBody @Validated CompanyAccountDto companyAccountDto,
HttpServletRequest req) throws ErrorException, InvalidKeySpecException, NoSuchAlgorithmException
是否有任何功能可用于在过滤器内接收此信息?
Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
String companyId = (String)pathVariables.get("companyId");
String accountId= (String)pathVariables.get("accountId");
如果您指的是 Spring Web 过滤器链,则必须手动解析 servlet 请求中提供的 URL。 这是因为过滤器在实际控制器获取请求之前执行,然后执行映射。
更适合在 Spring 过滤器(拦截器)中执行此操作。要存储检索到的值以便稍后在控制器或服务部分中使用它,请考虑使用具有 Scope 请求的 Spring bean(请求范围为单个 HTTP 请求)。 下面是拦截器代码示例:
@Component
public class RequestInterceptor implements Filter {
private final RequestData requestData;
public RequestInterceptor(RequestInfo requestInfo) {
this.requestInfo = requestInfo;
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
//Get authorization
var authorization = request.getHeader(HttpHeaders.AUTHORIZATION);
//Get some path variables
var pathVariables = request.getHttpServletMapping().getMatchValue();
var partyId = pathVariables.substring(0, pathVariables.indexOf('/'));
//Store in the scoped bean
requestInfo.setPartyId(partyId);
filterChain.doFilter(servletRequest, servletResponse);
}
}
为了安全访问RequestData
bean 中的存储值,我建议始终使用 ThreadLocal 构造来保存托管值:
@Component
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class RequestData {
private final ThreadLocal<String> partyId = new ThreadLocal<>();
public String getPartyId() {
return partyId.get();
}
public void setPartyId(String partyId) {
this.partyId.set(partyId);
}
}
通过添加拦截器它会起作用。 问题的完整代码: https : //stackoverflow.com/a/65503332/2131816
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.