【问题标题】:enable security check to access wsdl url启用安全检查以访问 wsdl url
【发布时间】:2013-01-18 10:20:49
【问题描述】:

我在我的机器上创建了一个 Web 服务。它的网址是

http://localhost:8080/aaa/test?wsdl

我想为其启用一项功能。一旦用户在浏览器中输入 url,它应该要求提供凭据。是否可以在 Web 服务中完成。

如果是,有人可以指导如何实现它。

谢谢。

【问题讨论】:

  • 您可以基于 URL 模式添加 basic HTTP authentication,例如使用 spring 安全性,就像在这个问题中所做的那样:Spring Security HTTP Basic Authentication
  • @XaviLópez。谢谢。我需要在 spring.xml 中添加 URL 模式吗?你能提供一些相同的代码示例吗
  • 是的,您需要在您的applicationContext.xml 中添加<http> 元素。查看example here

标签: java web-services http-authentication


【解决方案1】:

如果您已经在使用 Spring,则可以使用 Spring Security 轻松地将 basic authentication 应用于特定的 URL 模式。在您的applicationContext.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.0.3.xsd">

<!-- HTTP basic authentication in Spring Security -->
<http>
    <intercept-url pattern="/*wsdl?" access="ROLE_USER" />
    <http-basic />
</http>

<authentication-manager>
   <authentication-provider>
       <user-service>
       <user name="someUser" password="somePassword" authorities="ROLE_USER" />
       </user-service>
   </authentication-provider>
</authentication-manager>

</beans:beans>

示例取自Mkyong's Spring Security HTTP Basic Authentication Example

如果您想在数据库中查找用户,则需要使用不同的身份验证提供程序。如果您想查询standard Spring Security user data tablesSpring Security reference 会提到data-source-ref。如果您已经有了自己的结构,您可能有兴趣改用user-service-ref,您可以自己在其中查找用户。

<authentication-manager>
  <authentication-provider user-service-ref='myUserDetailsService'/>
</authentication-manager>

<beans:bean id="myUserDetailsService"
    class="mypackage.MyUserDetailsService">
  <beans:property name="dataSource" ref="dataSource"/>
</beans:bean>

代码mypackage.MyUserDetailsService扩展JdbcDaoImpl并实现UserDetailsService

【讨论】:

  • 感谢您的解决方案。如果我们必须从数据库中获取用户名和密码,它会有所帮助吗?如果是的话,如果你也告诉我会很棒
  • 您需要提供不同的authentication provider。您可以使用jdbc-user-service,它将查询数据库中的一些预定义表。或者您也可以使用user-service-ref 进行自定义用户查找。 Mkyong 在使用jdbc-user-service 时也有很好的example
  • 您是否尝试过将文件命名为applicationContext.xml?如果我没记错的话,这是 Spring 寻找的默认文件。
  • 哦,也许可以将&lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;/listener&gt; 添加到您的web.xml
猜你喜欢
  • 2017-12-29
  • 2023-03-16
  • 1970-01-01
  • 2014-07-31
  • 2013-06-20
  • 2011-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多