【问题标题】:Spring security removes RemoteUser from httpServletRequestSpring security 从 httpServletRequest 中删除 RemoteUser
【发布时间】:2018-04-06 12:38:57
【问题描述】:

尝试使用 apache 实现 sso。在 apache 中添加 auth 并通过 ajp 在 Spring Boot 应用程序 httpServletRequest.getRemoteUser() 中设置用户名。

一旦将 spring 安全依赖添加到启动项目中,就无法从 httpRequest 获取远程用户。

@GetMapping("/sso")
    public String test(HttpServletRequest request, @RequestHeader Map<String, String> headers) {

        String u1 = request.getRemoteUser();

        return "--" + u1 + "--" + headers.toString();
    }

u1 给出未添加 spring 安全依赖时从 Apache 发送的用户名

在 spring 安全配置中允许所有请求

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().permitAll();
    }

在 Apache 中,通过命令添加了一个用户,并在虚拟主机中添加了以下几行以进行身份​​验证

        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user

【问题讨论】:

    标签: spring-boot spring-security


    【解决方案1】:

    终于有办法通过spring security从httpRequest.getRemoteUser()获取用户了。

    在 spring 安全初始化之前添加了一个过滤器来调用,并将远程用户保存在请求范围内以供功能使用。

    注册过滤器

        @Bean
        public FilterRegistrationBean registerRequestLogFilter(SSOFilter filter) {
            FilterRegistrationBean reg = new FilterRegistrationBean(filter);
            reg.setOrder(3);
            return reg;
        }
    

    并添加属性

    security.filter-order=5
    

    从过滤器将用户值保存在请求分数中,稍后使用它进行身份验证。

    【讨论】:

      猜你喜欢
      • 2012-04-07
      • 2016-05-11
      • 2016-07-21
      • 1970-01-01
      • 2013-04-03
      • 1970-01-01
      • 2019-12-24
      • 2014-01-19
      • 1970-01-01
      相关资源
      最近更新 更多