当前位置: 首页 > 知识库问答 >
问题:

Spring boot oauth2管理httpbasic身份验证

方权
2023-03-14
management.context-path=/admin/actuators
management.security.enabled=true
management.security.role=ADMIN

security.user.name=admin
security.user.password=password
<oauth>
<error_description>
Full authentication is required to access this resource
</error_description>
<error>unauthorized</error>
</oauth>

让OAUTH2和管理endpoint工作的正确方法是什么?

共有1个答案

姬选
2023-03-14

问题是@enableResourceServer导入的ResourceServerConfiguration的阶数为3,远高于ManagementServerProperties.access_override_order
有关执行器安全性和排序配置类的Spring Boot文档:http://docs.Spring.io/spring-boot/docs/1.4.3.release/reference/htmlsingle/#boot-features-security-actactor

默认的执行器安全配置比只允许访问/healtendpoint并阻止其馀endpoint要聪明得多,它实际上会根据management.portmanagement.contextpath进行更改,而且要找到正确的管理endpointURL,而不给您的安全性留下漏洞或扰乱您自己的资源可能会非常困难。

如果要保持自动配置的管理安全性的优点,请选择两个选项:

@Component
@Slf4j
public class ResourceServerConfigurationPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof ResourceServerConfiguration) {
            LOGGER.debug("Lowering order of ResourceServerConfiguration bean : {}", beanName);
            ResourceServerConfiguration config = (ResourceServerConfiguration) bean;
            config.setOrder(SecurityProperties.ACCESS_OVERRIDE_ORDER);
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

}
/** 
 * Extend the default resource server config class, and downgrade its order
 */
public class ResourceServerLowPrecedenceConfiguration extends ResourceServerConfiguration {

     /**
     * This is enough to override Spring Boot's default resource security,
     * but it does not takes over the management.
     */
    @Override
    public int getOrder() {
        return SecurityProperties.ACCESS_OVERRIDE_ORDER;
    }
}

和您自己的配置类:

/** @EnableResourceServer is replaced by @Import using the low precedence config */
@Configuration
@Import(ResourceServerLowPrecedenceConfiguration.class)
public class YourOwnOAuth2Config extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(final HttpSecurity http) throws Exception {
        // Secure your resources using OAuth 2.0 here
    }
}

编辑:您还可以重写自己的@enableResourceServer批注,以快捷方式执行@import:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ResourceServerLowPrecedenceConfiguration.class)
public @interface EnableResourceServer {
}

IMHO当spring-security-oauth位于类路径上时,这应该是默认行为。
请参阅关于GitHub问题的讨论:https://GitHub.com/spring-projects/spring-boot/issues/5072

 类似资料:
  • spring-security.xml: 在新泽西行动豆的内部是这样的: 整个应用程序构建良好,jetty服务器启动没有问题(MyAuthenticationManager和org.springframework.security.AuthenticationManager都成功地预安装了),但是当尝试使用autowired authmanager时,我得到了空指针。

  • 我正在使用Vert。后端为x,前端为AngularJS。 Vert. x服务器使用POST和GET方法接收HTTP操作。不知何故,我为每个请求获取不同的会话ID。 以下是来自我的LoginFormHandler类句柄例程的代码片段。 我正在将用户对象放入当前会话中。然后我移动到新页面并向Vert. x服务器发送POST请求。在那个POST处理程序中,我正在尝试获取会话对象: 我没有得到用户。此外,

  • ii)$jboss_home/bin/kcadm.sh配置凭据--服务器http://localhost:8080/auth--realm master-user uadmin--password${UADMIN_PWD} iii)$jboss_home/bin/kcadm.sh create realms-s realm=myrealm-s enabled=true 最重要的是- 对于领域创建过

  • Yubikey PIV 管理器 Yubikey PIV 管理器是一个可以管理 PIV 相关内容的图形界面管理工具。它的命令行版本可以使用 Homebrew 来安装【译者注:macOS 平台】: ❯ brew install yubico-piv-tool 命令行版本的工具拥有更加强大的功能(例如 允许覆盖 PIN 和修改默认插槽的触摸策略),但它更容易导致用户误操作。(它很容易会覆盖现有密钥) 完

  • 我在调试身份验证问题时遇到了这个代码片段: 我在调试和摆弄用户凭证时注意到,如果第一个身份验证提供者(即< code > userdailsservice )无法对我的用户进行身份验证,那么就会远程调用我的LDAP服务器来尝试对我的用户进行身份验证。但是,如果第一个身份验证提供者成功地对我的用户进行了身份验证,则不会调用第二个身份验证提供者。 我的问题是,列出这些身份验证提供者的工作方式是否使得如

  • 我有Springmvc应用程序。我添加了带有CustomAuthentiationManager的Spring Security。这是appC中包含的application-security.xmlontex.xml.login.jsp我使用带有2个输入的标准登录页面:name='j_username'