2
我無法獲得tomcat
配置的權利。spring啓動tomcat J2EE預認證認證
我想在Tomcat
上部署簡單的Spring Boot應用程序,j2eePreAuth
爲Tomcat
以進行身份驗證。
我讀了一些關於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();
}
}
我得到它的工作! –