gpt4 book ai didi

java - 如何使用 Spring 安全更新过滤器的 header 参数?

转载 作者:行者123 更新时间:2023-11-30 07:50:34 25 4
gpt4 key购买 nike

我正在开发一个具有 Spring 安全性的过滤器,它扩展OncePerRequestFilter 类,它必须更新 REST 服务中的参数。参数由带有注释 @RequestHeader 的 header 输入。

我尝试使用以下类更新过滤器的参数:

public class HeaderMapRequestWrapper extends HttpServletRequestWrapper {
/**
* construct a wrapper for this request
*
* @param request
*/
public HeaderMapRequestWrapper(HttpServletRequest request) {
super(request);
}

private Map<String, String> headerMap = new HashMap<String, String>();

/**
* add a header with given name and value
*
* @param name
* @param value
*/
public void addHeader(String name, String value) {
headerMap.put(name, value);
}

public void removeteHeader(String name){
headerMap.remove(name);
}
@Override
public String getHeader(String name) {
String headerValue = super.getHeader(name);
if (headerMap.containsKey(name)) {
headerValue = headerMap.get(name);
}
return headerValue;
}

/**
* get the Header names
*/
@Override
public Enumeration<String> getHeaderNames() {
List<String> names = Collections.list(super.getHeaderNames());
for (String name : headerMap.keySet()) {
names.add(name);
}
return Collections.enumeration(names);
}

@Override
public Enumeration<String> getHeaders(String name) {
List<String> values = Collections.list(super.getHeaders(name));
if (headerMap.containsKey(name)) {
values.add(headerMap.get(name));
}
return Collections.enumeration(values);
}

}

并且使用方法 .addHeader("parameter", "New value"),但是当我在方法中读取参数时它没有改变,但是如果从 .getHeader("parameter") 方法来自 HttpServletRequest 类,我在同一个方法类中输入了它。更改已完成,但方法参数未出现。

方法如下:

@Autowired
HttpServletRequest a;


//Annotations @GetMapping......
public void method (@RequestHeader(value="Parameter") String parameter){
System.out.print(parameter); //Parameter did not change
system.out.print(a.getHeader("parameter")); //Parameter changed.
}

有人知道如何从过滤器或其他方式更改操作参数....

最佳答案

我不确定你做错了什么,但当我用我的设置尝试它时,它似乎有效。请看下面的代码。

/**
* username: test, password: test
* Added Header: param
* REST URL: http://localhost:8080/hello
*/

package com.test;

import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.filter.OncePerRequestFilter;

@SpringBootApplication
public class TestRequestWrapperApplication {

public static void main(String[] args) {
SpringApplication.run(TestRequestWrapperApplication.class, args);
}
}

@Component
class RequestWrapperFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
HeaderMapRequestWrapper wrappedRequest = new HeaderMapRequestWrapper((HttpServletRequest)request);
wrappedRequest.addHeader("param", "Hello World!");

filterChain.doFilter(wrappedRequest, response);
}
}

@RestController
class TestRest {
@GetMapping("hello")
public String hello(@RequestHeader("param") String param) {
return "param: " + param;
}
}

@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("test").authorities("test").password("test");
}
}

class HeaderMapRequestWrapper extends HttpServletRequestWrapper {
public HeaderMapRequestWrapper(HttpServletRequest request) {
super(request);
}

private Map<String, String> headerMap = new HashMap<String, String>();

public void addHeader(String name, String value) {
headerMap.put(name, value);
}

public void removeteHeader(String name) {
headerMap.remove(name);
}

@Override
public String getHeader(String name) {
String headerValue = super.getHeader(name);
if (headerMap.containsKey(name)) {
headerValue = headerMap.get(name);
}
return headerValue;
}

@Override
public Enumeration<String> getHeaderNames() {
List<String> names = Collections.list(super.getHeaderNames());
for (String name : headerMap.keySet()) {
names.add(name);
}
return Collections.enumeration(names);
}

@Override
public Enumeration<String> getHeaders(String name) {
List<String> values = Collections.list(super.getHeaders(name));
if (headerMap.containsKey(name)) {
values.add(headerMap.get(name));
}
return Collections.enumeration(values);
}

}

关于java - 如何使用 Spring 安全更新过滤器的 header 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47678337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com