2017-08-24 29 views
2

tomcatの設定が正しくできません。spring boot J2EE事前認証済みの認証

Tomcatに簡単なSpringブートアプリケーションをインストールして、Tomcatj2eePreAuthをauthにしたいと考えています。

私はweb.xmlについて何かを読んでそれを構成しました。彼らは、セキュリティ設定をSpringクラスとは別にweb.xmlに入れることに言及しています。しかしそれは何も変わらなかった。

またweb.xmlTomcat自体を変更しようとしましたが、成功しませんでした。

私の質問は: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; 
    } 

} 

とシンプルREStコントローラー::私はそれが動作するようになった

@RestController 
@RequestMapping(value = "/a") 
@PreAuthorize("hasAuthority('ROLE_USER')") 
public class Controller { 

    @RequestMapping("") 
    public String index(Principal p) { 
     return "logged in as: " + p.getName(); 
    } 

} 
+0

を、以下の追加! –

答えて

0

は、ここに私のセキュリティです!

私はそれが動作するようになったTomcat Sの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> 
関連する問題