角色/权限无法在Websphere Liberty中工作

时间:2016-12-21 20:34:45

标签: spring-security websphere-liberty

我正在努力让Spring安全角色与websphere自由一起工作。我知道我已经正确设置了自由,因为我编写了一个非常简单的servlet 3应用程序,基于角色的限制,并且它在具有相同角色限制的同一台服务器上工作。

以下是我的SecurityConfig的相关部分:

@Override
protected void configure(final HttpSecurity http) throws Exception {
    LOGGER.info("adding testing constraint");
    http.authorizeRequests()
            .anyRequest().authenticated()
            .and().httpBasic();

    if (appProperties.isContainerManaged()) {
        LOGGER.info("using container managed");
        http.jee().mappableRoles("TESTING", "ADMIN");
    }
    http.csrf().disable()
            .logout()
            .permitAll();
}

以上是在服务器日志中打印出“使用容器管理”,所以我知道它正在工作:)

在我的控制器中,我正在传递校长:

public String index(final Model model, final Principal principal, final HttpSession session,
                    final HttpServletRequest request) {

但是当我打电话时:

Authentication authentication = (Authentication) principal;
authentication.getAuthorities()

我一无所获。

以下是server.xml的相关部分:

<application type="war" id="security-sample" name="security-test"
         location="${server.config.dir}apps/security-sample.war">
   <application-bnd>
       <security-role name="TESTING">
           <user name="myuser" />
       </security-role>
   </application-bnd>
</application>

我挖得更深了一点。我将应用程序转换为使用WebSpherePreAuthenticatedProcessingFilter。 (我对这方面的文档很少感到震惊)。我有加载过滤器但它在Liberty上失败了:

javax.naming.NameNotFoundException:UserRegistry

这看起来是个已知问题:

https://www.ibm.com/developerworks/community/forums/html/topic?id=62b6761f-1ae4-42c3-847b-485acbd95730

据我所知,如果您使用容器管理的安全性,那么Spring几乎不支持Spring。您可以获取用户信息,但不能获取组/角色/权限信息。

更新:

我得到了更多,我现在可以让用户的组显示自由,但不是通过security-role映射的角色。

这就是诀窍。我创建了一个LibertyPreAuthenticatedWebAuthenticatedDetailsS​​ource,它获取了用户的组。我在这里使用了调用:http://www.ibm.com/support/knowledgecenter/SSD28V_8.5.5/com.ibm.websphere.wlp.core.doc/ae/rwlp_sec_apis.html来弄清楚如何为用户获取组。

现在我只需要弄清楚如何使用映射的安全角色......

1 个答案:

答案 0 :(得分:0)

我已经使用Java 11.0.8,Open Liberty 20.0.0.8和Spring Boot 2.3.1进行了此工作。这是相关的安全配置。 @DeclareRoles 是启用ROLE_ACTUATOR映射所必需的, .hasRole(“ ACTUATOR”)方法调用还不够。

@Configuration
@EnableWebSecurity(debug=true)
@EnableGlobalMethodSecurity(jsr250Enabled = true)
@DeclareRoles({"ROLE_SERVICE","ROLE_ACTUATOR"})
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(HttpSecurity http)
    throws Exception
    {
        http.csrf().disable()
            .authorizeRequests()
                .requestMatchers(EndpointRequest.toAnyEndpoint()).hasRole("ACTUATOR")
                .anyRequest().authenticated()
            .and()
                .httpBasic()
            .and()
                .jee().mappableRoles("ACTUATOR","SERVICE")
            ;
    }

server.xml的相关部分

<basicRegistry>
    <user name="wsAdmin" password="{hash}ATAAAAAI4h/5AWC+TLRAAAAAIOI2KiXXXDx7mesI2R99XTI1D1rskbx2IGeOsroCkqZh"/>
    <user name="wsService" password="{hash}ATAAAAAItVt5WPc0H/NAAAAAIGSCDtS7RkFRXeG6v3hOqUAkcAdLme5Pmx1bSNsk9kVN"/>
</basicRegistry>

<webApplication context-root="sample" type="war" location="C:\Deploy\sample-0.0.1-SNAPSHOT.war" >
    <application-bnd>
        <security-role name="ROLE_SERVICE">
            <user name="wsService" />
        </security-role>
        <security-role name="ROLE_ACTUATOR">
            <user name="wsAdmin" />
        </security-role>
    </application-bnd>
</webApplication>

为映射生成这些日志条目

[2020-09-29T14:55:50.212-0400] 0000002f SystemOut     O   2020-09-29 14:55:50.211 DEBUG 11460 --- [ecutor-thread-9] p.j.J2eePreAuthenticatedProcessingFilter : PreAuthenticated J2EE principal: wsService
[2020-09-29T14:55:50.213-0400] 0000002f SystemOut     O   2020-09-29 14:55:50.212 DEBUG 11460 --- [ecutor-thread-9] p.j.J2eePreAuthenticatedProcessingFilter : preAuthenticatedPrincipal = wsService, trying to authenticate
[2020-09-29T14:55:50.217-0400] 0000002f SystemOut     O   2020-09-29 14:55:50.217 DEBUG 11460 --- [ecutor-thread-9] henticatedWebAuthenticationDetailsSource : J2EE roles [[ROLE_SERVICE]] mapped to Granted Authorities: [[ROLE_SERVICE]]


[2020-09-29T14:56:43.558-0400] 00000035 SystemOut     O   2020-09-29 14:56:43.558 DEBUG 11460 --- [cutor-thread-13] p.j.J2eePreAuthenticatedProcessingFilter : PreAuthenticated J2EE principal: wsAdmin
[2020-09-29T14:56:43.558-0400] 00000035 SystemOut     O   2020-09-29 14:56:43.558 DEBUG 11460 --- [cutor-thread-13] p.j.J2eePreAuthenticatedProcessingFilter : preAuthenticatedPrincipal = wsAdmin, trying to authenticate
[2020-09-29T14:56:43.559-0400] 00000035 SystemOut     O   2020-09-29 14:56:43.559 DEBUG 11460 --- [cutor-thread-13] henticatedWebAuthenticationDetailsSource : J2EE roles [[ROLE_ACTUATOR]] mapped to Granted Authorities: [[ROLE_ACTUATOR]]