Ste*_*tti 6 spring spring-boot
我无法获得tomcat正确的配置。
Tomcat我想在forj2eePreAuth上部署一个简单的 Spring Boot 应用程序Tomcat来进行身份验证。
我读了一些关于 a 的内容web.xml来配置它。他们提到将安全配置放在web.xmlSpring 类旁边。但这并没有改变任何事情。
我也尝试过改变web.xml它Tomcat本身,但没有成功。
所以我的问题是:我需要配置什么才能Tomcat做到这一点?
这是我的安全:
\n\n@Configuration\n@EnableWebSecurity\npublic class SecurityConfig extends WebSecurityConfigurerAdapter {\n private static String ROLE_PREFIX = "ROLE_";\n\n @Override\n protected void configure(HttpSecurity http) throws Exception {\n http.authorizeRequests()\n // Alle weiteren Pfadsegmente sind f\xc3\xbcr User authentifiziert erreichbar\n .anyRequest().authenticated()\n .and()\n .jee()\n // Registrierung eines eigenen Jee PreAuthenticatedProcessingFilter\n .j2eePreAuthenticatedProcessingFilter(j2eePreAuthenticatedProcessingFilter());\n }\n\n @Bean\n @Override\n public AuthenticationManager authenticationManagerBean() throws Exception {\n return super.authenticationManagerBean();\n }\n\n /**\n * Um auf die web.xml zu verzichten muss ein ganzer J2eePreAuthenticatedProcessingFilter definiert werden. \n */\n @Bean\n public J2eePreAuthenticatedProcessingFilter j2eePreAuthenticatedProcessingFilter() throws Exception {\n J2eePreAuthenticatedProcessingFilter j2eePreAuthenticatedProcessingFilter = new J2eePreAuthenticatedProcessingFilter();\n j2eePreAuthenticatedProcessingFilter.setAuthenticationManager(authenticationManagerBean());\n\n J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource = new J2eeBasedPreAuthenticatedWebAuthenticationDetailsSource();\n j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.setMappableRolesRetriever(simpleMappableAttributesRetriever());\n\n SimpleAttributes2GrantedAuthoritiesMapper simpleAttributes2GrantedAuthoritiesMapper = new SimpleAttributes2GrantedAuthoritiesMapper();\n simpleAttributes2GrantedAuthoritiesMapper.setConvertAttributeToUpperCase(true);\n j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource.setUserRoles2GrantedAuthoritiesMapper(simpleAttributes2GrantedAuthoritiesMapper);\n\n j2eePreAuthenticatedProcessingFilter.setAuthenticationDetailsSource(j2eeBasedPreAuthenticatedWebAuthenticationDetailsSource);\n return j2eePreAuthenticatedProcessingFilter;\n }\n\n /**\n * Dieser MappableAttributesRetriever liefert eine eigene Liste von JEE Rollen statt der aus einer web.xml.\n */\n @Bean\n public MappableAttributesRetriever simpleMappableAttributesRetriever() {\n SimpleMappableAttributesRetriever simpleMappableAttributesRetriever = new SimpleMappableAttributesRetriever();\n Set<String> roles = new HashSet<String>();\n // Hier m\xc3\xbcssen die Rollen angegeben werden!\n roles.add(ROLE_PREFIX + "INTERNAL");\n roles.add(ROLE_PREFIX + "MANAGEMENT");\n roles.add(ROLE_PREFIX + "USER");\n simpleMappableAttributesRetriever.setMappableAttributes(roles);\n return simpleMappableAttributesRetriever;\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n和一个简单的RESt控制器:
@RestController\n@RequestMapping(value = "/a")\n@PreAuthorize("hasAuthority(\'ROLE_USER\')")\npublic class Controller {\n\n @RequestMapping("")\n public String index(Principal p) {\n return "logged in as: " + p.getName();\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n
我让它工作了!
我将以下内容添加到Tomcats 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1175 次 |
| 最近记录: |