【问题标题】:Integrating Token based security into existing Spring Security web application将基于令牌的安全性集成到现有的 Spring Security Web 应用程序中
【发布时间】:2013-09-20 13:44:18
【问题描述】:

我正在设计一个 RESTful Web 服务,用户需要在经过适当的身份验证后才能访问该服务。我已经使用 Spring Security 3.0 为我的应用程序开发了安全性。现在我想集成 TokenBasedAuthentication。但是我卡在这里我该怎么做。

我的 ApplicationContextSecurity.xml:

<global-method-security pre-post-annotations="enabled">
    </global-method-security>
    <beans:bean id="myAccessDecisionManager"
        class="com.app.security.MyAccessDecisionManager">
    </beans:bean>
    <http auto-config="true" once-per-request="true"
        access-decision-manager-ref="myAccessDecisionManager"       
        access-denied-page="/jsp/errorPage.jsp">
        <intercept-url pattern="/*.app" access="ROLE_ANONYMOUS" />
        <form-login login-page="/login.app"
            login-processing-url="/j_spring_security_check" default-target-url="/login/checking.app"
            authentication-failure-url="/login.app?login_error=1" />
        <logout logout-url="/j_spring_security_logout"
            logout-success-url="/login.app" invalidate-session="true" />
        <session-management invalid-session-url="/login.app"
            session-fixation-protection="newSession">
            <concurrency-control max-sessions="100"
                error-if-maximum-exceeded="false" />
        </session-management>
    </http>

    <authentication-manager alias="authenticationManager">
        <authentication-provider ref="customAuthenticationProvider"></authentication-provider>
    </authentication-manager>

    <beans:bean id="customAuthenticationProvider"
        class="com.app.security.CustomAuthenticationProvider">
    </beans:bean>

我的 CustomAuthenticationProvider :

public class CustomAuthenticationProvider implements AuthenticationProvider {

@Autowired
private ILoginService loginService;

protected final transient Log log = LogFactory.getLog(getClass());

public Authentication authenticate(Authentication authentication)
        throws AuthenticationException {

    UsernamePasswordAuthenticationToken usernamePassswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
            authentication.getPrincipal(), authentication.getCredentials());

    // Doing authentication process here and returning authentication token
    return usernamePassswordAuthenticationToken;
}

public boolean supports(Class<? extends Object> authentication) {
    return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}

我的要求是,

  • 当用户想第一次访问rest web服务时,他应该从header中向服务器提供用户名/密码。
  • 服务器将接受请求,检查身份验证并为特定时期的未来请求生成令牌。 我还需要客户端代码来了解如何访问安全的 Web 服务。 谢谢。

【问题讨论】:

    标签: spring spring-security restful-authentication


    【解决方案1】:

    当用户第一次想要访问 REST Web 服务时,他应该 从标头向服务器提供用户名/密码。

    服务器将接受请求,检查身份验证并生成 特定时期未来请求的令牌

    您可以使用 HTTP 标头或映射到 Spring MVC 控制器的普通 HTTP POST 请求来执行此操作(这是我们在应用程序中执行此操作的方式):

    @Controller
    public class AuthenticationController {
        @Autowired
        @Qualifier("authenticationManager")
        AuthenticationManager     authenticationManager;
    
        @Autowired
        SecurityContextRepository securityContextRepository;
    
        @RequestMapping(method = RequestMethod.POST, value = "/authenticate")
        public @ResponseBody String authenticate(@RequestParam final String username, @RequestParam final String password, final HttpServletRequest request, final HttpServletResponse response) {
            final UsernamePasswordAuthenticationToken authenticationRequest = new UsernamePasswordAuthenticationToken(username, password);
            final Authentication authenticationResult = this.authenticationManager.authenticate(authenticationRequest);
    
            final String token = <some randomly generated secure token>;
    
            final Authentication authentication = new MyAuthenticationToken(authenticationResult, token);
    
            SecurityContextHolder.getContext().setAuthentication(authentication);
    
            this.securityContextRepository.saveContext(SecurityContextHolder.getContext(), request, response);
    
            return token;
        }
    }
    

    完成此操作后,客户端应在每个后续请求的 HTTP 标头中发送令牌。

    我还需要客户端代码来了解如何访问安全的网络服务

    不确定您在此处寻找的具体内容。如果您的客户端是在 Web 浏览器中运行的 JavaScript 库,那么将身份验证令牌设置为每个请求的 HTTP 标头应该很简单。如果您的客户端是设备,则该设备可以将令牌存储在内存中,并将其作为 HTTP 标头包含在每个请求中,使用您用来调用服务的任何 HTTP 客户端库。

    【讨论】:

      猜你喜欢
      • 2018-02-08
      • 2016-05-15
      • 2018-05-17
      • 2016-11-19
      • 2020-02-17
      • 2013-03-20
      • 2021-11-19
      • 1970-01-01
      • 2018-04-04
      相关资源
      最近更新 更多