【问题标题】:getting http 403 access denied for spring rest service春季休息服务拒绝http 403访问
【发布时间】:2018-03-16 05:21:21
【问题描述】:

我的 Spring REST 有以下配置,我希望我的所有 Web 服务都通过基本身份验证:

spring-sec.xml

 <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/*" access="hasRole('ROLE_ADMIN')"/>
        <http-basic/>
    </http>

    <!-- Authentication manager -->
    <authentication-manager>
        <authentication-provider>
        <user-service>
            <user name="admin" password="admin" authorities="ROLE_ADMIN"/>
            <user name="rest" password="rest" authorities="ROLE_REST"/>
            <user name="user" password="user" authorities="ROLE_USER"/>
         </user-service>
        </authentication-provider>
    </authentication-manager>

对于我的客户端代码,我有以下内容:

String encoding = Base64.getEncoder().encodeToString(("admin:admin").getBytes());

        String authHeader = "Basic " + encoding;

        System.out.println(authHeader);

        HttpPost post = new HttpPost(uri);
        post.setHeader("Content-Type", "application/octet-stream; boundary=\"---Content Boundary\"");
        post.setHeader("Authorization", authHeader);

我的客户端的所有执行都被拒绝 http 403 访问,但是从代码中,我已经在使用配置的用户名和密码。我也从 hasRole 更改为 hasAuthority,但无济于事。请帮助解决这个问题。谢谢!

web.xml:

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/spring-sec.xml</param-value>
    </context-param>
       <!-- Spring Security filter -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>   

【问题讨论】:

  • 在这里感谢您的帮助,谢谢!

标签: rest spring-security basic-authentication


【解决方案1】:

设法在我的 spring security xml 配置中找到了问题及其。从 403 错误中,指出“无法验证提供的 CSRF 令牌,因为在 Spring Security 中找不到您的会话”。

由于 web 服务只被非浏览器客户端使用,我在 xml 配置中禁用了 csrf,然后它就像一个魅力!

只是想知道这是否会保存到我的网络服务中?怎么一个csrf函数?

【讨论】:

    猜你喜欢
    • 2015-03-15
    • 2016-01-21
    • 2011-07-26
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2016-11-13
    • 2018-06-19
    • 2016-07-19
    相关资源
    最近更新 更多