gpt4 book ai didi

Spring 4 Autowiring 到过滤器

转载 作者:行者123 更新时间:2023-12-01 15:06:52 24 4
gpt4 key购买 nike

我用 Spring 4.1.6 构建应用程序,我在 java 类中有所有配置。我的 AppConfig:

    @Configuration
@EnableWebMvc
@ComponentScan(basePackages = "pl.wrweb.springrest")
public class AppConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource getMessageSource() {
ReloadableResourceBundleMessageSource resource = new ReloadableResourceBundleMessageSource();
resource.setBasename("classpath:messages");
resource.setDefaultEncoding("UTF-8");
return resource;
}

}

AppInitializer:

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

public void onStartup(ServletContext container) throws ServletException {


AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");

container.addFilter("customFilter", new DelegatingFilterProxy(new CustomFilter())).addMappingForUrlPatterns(null, true, "/*");

}

@Override
protected String[] getServletMappings() {
return new String[0];
}


@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[0];
}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[0];
}
}

和我的 CustomFilter:

@Configuration
@EnableWebSecurity
public class CustomFilter implements Filter {

@Autowired
UserService userService;

public void init(FilterConfig fc) throws ServletException {

}

public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws IOException, ServletException {

HttpServletRequest httpServletRequest = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

if (httpServletRequest.getRequestURI().contains("/employee/")) {
String token = httpServletRequest.getHeader("x-token");
System.out.println("token " + token);
Long userId = TokenUtils.getUserNameFromToken(token);
System.out.println("ObjectStore.token " + ObjectStore.token);

if (token!=null) {
if (token.equals(String.valueOf(ObjectStore.token))) {
int index = ObjectStore.token++;
ObjectStore.token = index+1;
System.out.println("index " +ObjectStore.token + "token " + token);
response.setHeader("x-token", String.valueOf(ObjectStore.token));
fc.doFilter(req, response);
} else {
response.sendError(HttpStatus.FORBIDDEN.value());
fc.doFilter(req, response);
return;
}
} else {
response.sendError(HttpStatus.FORBIDDEN.value());
fc.doFilter(req, response);
return;
}
} else {
fc.doFilter(req, res);
}
}

public void destroy() {

}

}

一切正常,但我无法注入(inject) userService,它为空,我需要它来检查请求中的 token 。

最佳答案

你应该像这样使用代码!

private UserService userService;

然后添加这段代码

public void init(FilterConfig fc) throws ServletException {
ApplicationContext app = WebApplicationContextUtils.getRequiredWebApplicationContext(fc.getServletContext());
userService= app.getBean(UserService.class);
}

关于Spring 4 Autowiring 到过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31011280/

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