自定义领域在Spring Boot应用程序中未被调用

huangapple 未分类评论46阅读模式
英文:

Custom Realm not invoked in the Spring Boot Application

问题

Spring Boot版本:2.2.6.RELEASE

我正在将一个旧的应用程序迁移到Spring Boot。在旧的应用程序中,在Tomcat的server.xml中,我有以下Realm配置:

<Realm className="com.test.realm.AuthServletRealm" authServletUrl="http://localhost:8080/dh-services/auth" allowInternalToken="true" tokenValidationPeriod="60000"/>
<Host name="localhost"  appBase="webapps"
      unpackWARs="true" autoDeploy="true">
    <Valve className="com.test.realm.AuthServletValve" />
    <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
    <Valve className="org.apache.catalina.valves.ErrorReportValve" showReport="false" showServerInfo="false" />
</Host>

我正在尝试将这个配置转换为一个Spring配置类,代码如下:

@Configuration
public class TomcatConfiguration {

    @Bean
    public ConfigurableServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addContextCustomizers(TomcatConfiguration::customize);
        factory.addEngineValves(new SingleSignOn());
        factory.addEngineValves(new ErrorReportValve());
        factory.addContextValves(new AuthServletValve());
        return factory;
    }

    private static void customize(Context context) {
        context.setRealm(new AuthServletRealm());
        context.addConstraint(createSecurityConstraint());
    }

    private static SecurityConstraint createSecurityConstraint() {
        SecurityConstraint securityConstraint = new SecurityConstraint();
        SecurityCollection securityCollection = new SecurityCollection();
        securityCollection.addPattern("/*");
        securityConstraint.addCollection(securityCollection);
        return securityConstraint;
    }
}

阀门被正确调用,但是我的AuthServletValve没有被调用。我期望在每次调用我的Spring Boot API时,AuthServletValve被调用以获取主体。但什么都没有发生。

英文:

Spring boot version: 2.2.6.RELEASE

I'm migrating a legacy application to the Spring Boot.
In the legacy application, in the server.xml of the Tomcat, I had the following Realm configuration:

<Realm className="com.test.realm.AuthServletRealm" authServletUrl="http://localhost:8080/dh-services/auth" allowInternalToken="true" tokenValidationPeriod="60000"/>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">

            &lt;Valve className=&quot;com.test.realm.AuthServletValve&quot; /&gt;

            &lt;Valve className=&quot;org.apache.catalina.authenticator.SingleSignOn&quot; /&gt;

            &lt;Valve className=&quot;org.apache.catalina.valves.ErrorReportValve&quot; showReport=&quot;false&quot; showServerInfo=&quot;false&quot; /&gt;
        &lt;/Host&gt;

I'm trying to convert this configuration in a spring configuration class with the following code:

@Configuration
public class TomcatConfiguration {

    @Bean
    public ConfigurableServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
        factory.addContextCustomizers(TomcatConfiguration::customize);
        factory.addEngineValves(new SingleSignOn());
        factory.addEngineValves(new ErrorReportValve());
        factory.addContextValves(new AuthServletValve());
        return factory;
    }

    private static void customize(Context context) {
        context.setRealm(new AuthServletRealm());
        context.addConstraint(createSecurityConstraint());
    }


    private static SecurityConstraint createSecurityConstraint() {
        SecurityConstraint securityConstraint = new SecurityConstraint();
        SecurityCollection securityCollection = new SecurityCollection();
        securityCollection.addPattern(&quot;/*&quot;);
        securityConstraint.addCollection(securityCollection);
        return securityConstraint;
    }
}

Valves are invoked correctly, but my AuthServletValve is not invoked.
I expect that for every invocation of my Spring Boot API, AuthServletValve is invoked to get the principal. But nothing

huangapple
  • 本文由 发表于 2020年5月5日 21:02:04
  • 转载请务必保留本文链接:https://java.coder-hub.com/61613816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定