2017-08-24 104 views
2

我無法獲得tomcat配置的權利。spring啓動tomcat J2EE預認證認證

我想在Tomcat上部署簡單的Spring Boot應用程序,j2eePreAuthTomcat以進行身份​​驗證。

我讀了一些關於web.xml的配置。他們提到除了Spring類之外,還要把安全配置放到一個web.xml之內。但它沒有改變任何東西。

我也試圖改變Tomcat本身的web.xml沒有成功。

所以我的問題是:我有什麼配置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小號的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>