混合xml环境中使用Java配置和MvcWebApplicationInitializers的RestServices的Spring安全性
我在这个论坛上搜索了很多次,没有找到这个问题的答案 我需要使用基本授权来保护我的REST服务。我的SecurityConfig类如下所示,并正确加载。当应用程序加载到Websphere中时,将显示日志语句混合xml环境中使用Java配置和MvcWebApplicationInitializers的RestServices的Spring安全性,java,spring,Java,Spring,我在这个论坛上搜索了很多次,没有找到这个问题的答案 我需要使用基本授权来保护我的REST服务。我的SecurityConfig类如下所示,并正确加载。当应用程序加载到Websphere中时,将显示日志语句 @EnableWebMvcSecurity @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { private static final Logger LOG = Logg
@EnableWebMvcSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
private static final Logger LOG = LoggerFactory
.getLogger(WebSecurityConfigurerAdapter.class);
@Autowired
AppUserAuthService auas;
@Autowired
AuthenticationProviderImpl auai;
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
LOG.debug("getting into configureGlobal");
LOG.debug(auas.toString());
LOG.debug(auai.toString());
auth
.userDetailsService(auas)
.and()
.authenticationProvider(auai) ;
}
@Override
public void configure(HttpSecurity http) {
LOG.debug("getting into configure");
try {
http.httpBasic().and()
.authorizeRequests().anyRequest().authenticated();
http.csrf()
.disable()
.antMatcher("/service/**") ;
} catch (Exception e) {
throw new SecurityWrapperException("configure() blowing up when trying to match /service/**" ,e);
}
}
不过,我的REST调用没有通过基本身份验证进行检查,因为我还没有弄清楚如何将其连接到Servlet初始值设定项
问题是下面所有的配置类连接都是在各种ApplicationContext.xml文件中完成的。然而,我似乎需要覆盖下面的方法。我的问题是我是否必须将所有内容转换为JavaConfiguration,或者我是否可以引用应用程序上下文文件正在使用的适当类。。。如果是,他们的名字是什么?
例如,我知道下面的getRootConfigClasses方法需要一些“RootConfig”.class
`public class MvcWebApplicationInitializers
extends
AbstractAnnotationConfigDispatcherServletInitializer {`enter code here`
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return new Class[] { SecurityConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return null;
}`
`公共类MVCWebApplication初始化器
延伸
AbstractAnnotationConfigDispatcherServletInitializer{`在此处输入代码`
@凌驾
受保护类[]getRootConfigClasses(){
//TODO自动生成的方法存根
返回新类[]{SecurityConfig.Class};
}
@凌驾
受保护类[]getServletConfigClasses(){
//TODO自动生成的方法存根
返回null;
}
@凌驾
受保护的字符串[]getServletMappings(){
//TODO自动生成的方法存根
返回null;
}`
我想返回ApplicationContext.xmls填充的正确类。显然,为这些方法返回null不是一件好事。一般来说,将xml与JavaConfiguration混合使用是否合理? 在Servlet 3.0+环境中,您可以选择配置 Servlet容器以编程方式作为替代或组合使用 使用web.xml文件。下面是注册 DispatcherServlet:
因此,您仍然可以使用xml dispatcher configuration IMO,正如Anadi Misra所解释的,在spring中混合java配置和xml是没有问题的。方法取决于您是否使用web.xml。是吗?谢谢您的回答。我现在确实使用了web.xml文件,这就是我加载ApplicationContext.xml以及设置Dispatch Servlet的方式d Spring MVC。我正在尝试注册我的SecurtiyConfig.class,以便Servlet引擎在我的Rest服务上额外使用它来强制执行SpringSecurity。看起来MvcWebApplicationInitializers可以工作,但我需要知道@Override方法使用什么类。或者,如果有办法在将web.xml与Servlet相关联。我可以看到您在初始级别记录初始化。如果Spring security filter链处于活动状态,它也应该提供调试日志。您在接收请求时是否看到这些日志?
import org.springframework.web.WebApplicationInitializer;
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
XmlWebApplicationContext appContext = new XmlWebApplicationContext();
appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext));
registration.setLoadOnStartup(1);
registration.addMapping("/");
}
}