【问题标题】:Spring Security, how to handle web-service based authenticationSpring Security,如何处理基于 Web 服务的身份验证
【发布时间】:2016-04-02 06:02:18
【问题描述】:

我们正在尝试在我们的 webflow 应用程序中使用 Spring Security 实现身份验证和授权。有一个服务将验证用户并返回用户的相应角色。

但是在这个调用之后就可以在我们的应用程序中使用 Spring Security 配置,因为所有与 Spring Security 相关的文档都告诉我们需要在 xml 中预定义用户名、密码和角色,属性文件或数据库。

我可以在会话中存储返回的滚动并完全避免弹簧安全性。但我想对应用程序实施更多的安全性并让黑客远离。

那么在这种情况下可以使用 Spring Security 吗?另外,在会话中保留角色的想法如何?

请在下面找到示例 Spring 安全 xml。是否可以在身份验证、运行时为 uname/psswrd/role 分配值。这样所有后续调用都将由 spring security 处理。

<security:http auto-config="true">
<security:form-login login-page="/spring/login"
                     login-processing-url="/spring/loginProcess"
                     default-target-url="/spring/main"
                     authentication-failure-url="/spring/login?login_error=1" />
<security:logout logout-url="/spring/logout" logout-success-url="/spring/logout-success" />
</security:http>

<security:authentication-provider>
<security:password-encoder hash="md5" />
<security:user-service>
    <security:user name="keith" password="417c7382b16c395bc25b5da1398cf076"
                   authorities="ROLE_USER,ROLE_SUPERVISOR" />
    <security:user name="erwin" password="12430911a8af075c6f41c6976af22b09"
                   authorities="ROLE_USER,ROLE_SUPERVISOR" />
    <security:user name="jeremy" password="57c6cbff0d421449be820763f03139eb"
                   authorities="ROLE_USER" />
    <security:user name="scott" password="942f2339bf50796de535a384f0d1af3e"
                   authorities="ROLE_USER" />
</security:user-service>
</security:authentication-provider>

http://www.baeldung.com/spring-security-authentication-provider,这会涵盖我的场景吗?

【问题讨论】:

  • 您写道:“有一项服务将验证用户并返回用户的相应角色。” -- 你的应用程序是否调用了这个服务并将登录名和密码提交给了这个服务,或者用户是如何认证的?
  • 是的,我们首先调用服务进行身份验证。

标签: spring spring-mvc spring-security spring-webflow spring-security-oauth2


【解决方案1】:

您可以使用 Spring-Security。如果您可以调整您的服务,以便它提供密码而不是检查密码,那么您唯一需要做的就是实现自己的UserDetailsService

以 Spring Security 的配置为例,使用 JdbcDaoImpl-UserDetailsServiceInMemoryDaoImpl-UserDetailsService,然后将它们替换为调用 Web 服务的 UserDetailsService

如果您无法提供密码,那么您需要实现/配置您自己的AuthenticationProvider。要么使用RemoteAuthenticationProvider,要么实现AbstractUserDetailsAuthenticationProvider 的子类。

我推荐RemoteAuthenticationProvider方式:

<http>

    ...
    <security:authentication-provider ref="remoteAuthenticationProvider" />
  </http>


  <bean id="remoteAuthenticationProvider" class="org.springframework.security.authentication.rcp.RemoteAuthenticationProvider">
    <property name="remoteAuthenticationManager" ref="myRemoteAuthenticationManager" />
  </bean>

  <bean id="myRemoteAuthenticationManager" class="MyRemoteAuthenticationManager"/>

您只需实现实现RemoteAuthenticationManagerMyRemoteAuthenticationManager。接口RemoteAuthenticationManager只有一种方法:

Collection<? extends GrantedAuthority> attemptAuthentication(String username,
                                                             String password)
                                                       throws RemoteAuthenticationException;

在您的实现中,此方法必须调用您的网络服务。

【讨论】:

猜你喜欢
  • 2011-08-16
  • 2017-07-08
  • 2015-02-06
  • 2010-11-20
  • 2017-07-10
  • 2013-09-25
  • 1970-01-01
  • 2016-03-03
  • 2017-09-14
相关资源
最近更新 更多