spring boot tomcat J2EE预认证认证

时间:2017-08-24 09:24:41

标签: spring spring-boot

我无法正确配置write.xlsx(df, file = "Output/table.xlsx") write.csv(df, file = "Output/table.csv")

我想在var sentence = "My Name is David Bonds and" let prefix = "My Name is " sentence.removeSubrange(sentence.range(of: prefix)!) let array = sentence.components(separatedBy: " ") print("Name: ", array[0],array[1]) // Name: David Bonds 部署一个简单的Spring Boot应用程序,并tomcat Tomcat进行身份验证。

我读了一些j2eePreAuth来配置它。他们提到将安全配置放在Spring类的Tomcat之外。但它没有改变任何东西。

我还尝试更改web.xml本身的web.xml但没有成功。

所以我的问题是:我需要在web.xml配置什么才能做到这一点?

这是我的安全:

Tomcat

一个简单的Tomcat控制器:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    private static String ROLE_PREFIX = "ROLE_";

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            // Alle weiteren Pfadsegmente sind für User authentifiziert erreichbar
                .anyRequest().authenticated()
                .and()
            .jee()
            // Registrierung eines eigenen Jee PreAuthenticatedProcessingFilter
                .j2eePreAuthenticatedProcessingFilter(j2eePreAuthenticatedProcessingFilter());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * Um auf die web.xml zu verzichten muss ein ganzer J2eePreAuthenticatedProcessingFilter definiert werden. 
     */
    @Bean
    public J2eePreAuthenticatedProcessingFilter j2eePreAuthenticatedProcessingFilter() throws Exception {
        J2eePreAuthenticatedProcessingFilter j2eePreAuthenticatedProcessingFilter = new J2eePreAuthenticatedProcessingFilter();
        j2eePreAuthenticatedProcessingFilter.setAuthenticationManager(authenticationManagerBean());

        J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource = new J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource();
        j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.setMappableRolesRetriever(simpleMappableAttributesRetriever());

        SimpleAttributes2GrantedAuthoritiesMapper simpleAttributes2GrantedAuthoritiesMapper = new SimpleAttributes2GrantedAuthoritiesMapper();
        simpleAttributes2GrantedAuthoritiesMapper.setConvertAttributeToUpperCase(true);
        j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.setUserRoles2GrantedAuthoritiesMapper(simpleAttributes2GrantedAuthoritiesMapper);

        j2eePreAuthenticatedProcessingFilter.setAuthenticationDetailsSource(j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource);
        return j2eePreAuthenticatedProcessingFilter;
    }

    /**
     * Dieser MappableAttributesRetriever liefert eine eigene Liste von JEE Rollen statt der aus einer web.xml.
     */
    @Bean
    public MappableAttributesRetriever simpleMappableAttributesRetriever() {
        SimpleMappableAttributesRetriever simpleMappableAttributesRetriever = new SimpleMappableAttributesRetriever();
        Set<String> roles = new HashSet<String>();
        // Hier müssen die Rollen angegeben werden!
        roles.add(ROLE_PREFIX + "INTERNAL");
        roles.add(ROLE_PREFIX + "MANAGEMENT");
        roles.add(ROLE_PREFIX + "USER");
        simpleMappableAttributesRetriever.setMappableAttributes(roles);
        return simpleMappableAttributesRetriever;
    }

}

1 个答案:

答案 0 :(得分:1)

我得到了它的工作!

我在Tomcat的conf / web.xml

中添加了以下内容
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Basic Authentication</web-resource-name>
<!--Here wildcard entry defines authentication is needed for whole app -->
            <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>
    <auth-constraint>
        <role-name>ROLE_USER</role-name>
    </auth-constraint>
</security-constraint>

<login-config>
    <auth-method>BASIC</auth-method>
</login-config>

<security-role>
    <description>Any User</description>
    <role-name>ROLE_USER</role-name>
</security-role>