【问题标题】:How to secure a REST Web Service using Spring Security如何使用 Spring Security 保护 REST Web 服务
【发布时间】:2017-07-12 22:37:02
【问题描述】:

我正在尝试在 url /dispatcher/rest/** 处保护我的 REST Web 服务

如果通过浏览器访问 Web 服务,我当前的设计工作正常 - 当我尝试转到 REST url 时,它会将我重定向到登录页面以输入凭据,然后在登录后将我重定向到 Web 服务数据。

问题是,当我尝试使用 RestTemplate 通过 java 代码访问 Web 服务时,我的代码会中断。即使用户已经登录并进行了身份验证,也会发生这种情况。

我的 spring-security.xml 配置文件:

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <!-- enable use-expressions -->
    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/dispatcher/admin**" access="hasRole('ROLE_ADMIN')" />
 <intercept-url pattern="/dispatcher/rest/**" access="hasRole('ROLE_ADMIN')"/>
        <!-- access denied page -->
        <access-denied-handler error-page="/dispatcher/403" />
        <form-login 

            login-page="/dispatcher/login" 
            default-target-url="/dispatcher/admin"
            login-processing-url="/dispatcher/login_process"
            authentication-failure-url="/dispatcher/login?error" 
            username-parameter="username"
            password-parameter="password" />
        <logout logout-success-url="/dispatcher/login?logout" logout-url = "/dispatcher/logout"/>
        <!-- enable csrf protection -->
        <csrf />
        </http>

    <authentication-manager>
        <authentication-provider user-service-ref="myUserDetailsService" >

        </authentication-provider>
    </authentication-manager>
<beans:bean id="myUserDetailsService" class="com.shopping.services.MyUserDetailsService" />
</beans:beans>

任何帮助将不胜感激!

【问题讨论】:

  • 描述“当我尝试使用 RestTemplate 通过 java 代码访问 Web 服务时,我的代码会中断”。尝试找出问题所在,您的RestTemplate 相关代码已损坏,或者安全性已损坏。在第一种情况下,您还应该发布用于调用可能安全的 API 的代码。
  • 拥有一个受表单登录保护的 REST 服务是非常罕见的——您确定这就是您想要的吗?传统上,您将使用基本身份验证而不是表单登录,或者如果代表最终用户调用服务,您可以使用 OAuth2。

标签: spring web-services rest spring-security


【解决方案1】:

我找到的访问网络服务的解决方案如下:

 String username = ((UserDetails) principal).getUsername();
         String password = ((UserDetails) principal).getPassword();
        HttpClient client = new HttpClient();
        client.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
        restTemplate.setRequestFactory(new CommonsClientHttpRequestFactory(client));
        client.getState().setCredentials(AuthScope.ANY, defaultcreds);
        User x = restTemplate.getForObject("http://localhost:8080/Online_Shopping/dispatcher/rest/hello",User.class);

【讨论】:

    猜你喜欢
    • 2017-05-27
    • 2014-04-13
    • 1970-01-01
    • 2015-12-11
    • 2011-06-07
    • 2015-03-23
    • 2014-07-29
    • 2015-06-16
    • 2014-02-21
    相关资源
    最近更新 更多