java - 如何使用 Spring Java 配置转换 siteminder xml 配置

标签 java spring annotations spring-4 siteminder

我正在将基于旧版本的 Spring 应用程序转换为基于注解的 Spring4 应用程序。作为第一步,我将所有 xml 转换为基于 java 配置的注释。该应用程序运行良好,但唯一的问题是站点管理器 xml 配置。我不知道如何将 web.xml 中的以下 siteminder 配置转换为基于 java 的配置。

<login-config>
   <auth-method>CLIENT-CERT</auth-method>
   <realm-name>SiteMinderRealm</realm-name>
</login-config>

以上siteminder配置在web.xml中,

谁能告诉我如何在 AppInitializer.java 中为上述 xml 编写基于 java 的配置

我的web.xml及其对应的替换AppInitializer.java代码如下所示

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

    <display-name>SpringWebMVCApp</display-name>
    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.helloworld.config.AppConfig</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/rest/</url-pattern>
    </servlet-mapping>

    <login-config>
        <auth-method>CLIENT-CERT</auth-method>
        <realm-name>SiteMinderRealm</realm-name>
    </login-config>
</web-app>

AppInitializer.java

public class AppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/rest/");
    }

    private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(AppConfig.class);
        return context;
    }
}

更新 1

public class AppInitializer extends WebSecurityConfigurerAdapter implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher = servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/rest/");
    }

    private AnnotationConfigWebApplicationContext getContext() {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(AppConfig.class);
        return context;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
       http.httpBasic().realmName("SiteMinderRealm").and().x509();
    }

}

最佳答案

您是否尝试在 WebSecurityConfigurerAdapter 上配置它?像这样:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
   http.httpBasic().realmName("SiteMinderRealm").and().x509();
   }
}

关于java - 如何使用 Spring Java 配置转换 siteminder xml 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32455788/

相关文章:

java - 欺骗 JVM 转换数组

mysql - 显示客户下的所有订单的详细信息

java - @Resource 与 @Autowired

java - [示例]动态 fragment , fragment 间通信, fragment 重新添加到屏幕问题( fragment 保留)中

java - 使用aspectj进行Spring事务管理

java - 函数建议的变量列表

java - Mapstruct 更新值而不覆盖

spring - 对 Spring GenericBeanDefinition 的引用

javascript - 根据当前填充颜色更改注释填充

java - eclipse java 注释处理提交生成的带有 subclipse 的 java 类到 svn