我是 Spring启动框架的新手,我需要设置Subject bean并在Audit过滤器中打印它的值。
以下是我正在使用的课程:
Subject.java
public class MyManager {
@Autowired
private Subject subject;
public void SetSubject () {
subject = getSubject(); // Function return filled Subject bean having both values
//Printing subject here prints values for uid and tokenType
}
}
MyManager.java
public class Audit implements Filter {
@Autowired
private Subject subject;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println(Subject.toString()); // prints null
}
}
Audit.java
@Configuration
@ComponentScan(value = "com.org.project")
public class FilterConfig {
@Autowired
Environment environment;
@Bean
public FilterRegistrationBean oAuthRegistrationBean() {
final FilterRegistrationBean registration = new FilterRegistrationBean();
registration.addUrlPatterns("/*");
registration.addInitParameter("role", "uc:admin");
registration.setFilter(new AuthFilter()); //Subject and MyManager are part of this filter
return registration;
}
@Bean
public FilterRegistrationBean avrilAuditFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(getAuditFilter());
registration.addUrlPatterns("/*");
registration.setName("Audit");
return registration;
}
@Bean
public Filter getAuditFilter() {
return new Audit();
}
}
FilterConfig.java
var expiry = "06-19";
var stillValid = (new Date("01-" + expiry).getTime() > new Date().getTime());
在setSubject()函数中设置了Subject之后,在Audit过滤器类中获取了Subject的空值。
我有两个过滤器Auth和Audit。 First Auth过滤器进入了设置Subject bean的图片 其次,审计过滤器需要访问主题bean,在这种情况下 null