【问题标题】:Spring Rest Authentication with a response带有响应的 Spring Rest 身份验证
【发布时间】:2015-05-07 10:07:00
【问题描述】:

我已经设置了一个用于 REST 的安全上下文。配置如下

<!-- authentication manager and password hashing -->
    <authentication-manager alias="authenticationManager">
        <authentication-provider ref="daoAuthenticationProvider" />
    </authentication-manager>

    <beans:bean id="daoAuthenticationProvider"
        class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <beans:property name="userDetailsService" ref="userDetailsService" />
        <beans:property name="passwordEncoder" ref="passwordEncoder" />
    </beans:bean>

    <beans:bean id="userDetailsService" name="userAuthenticationProvider"
        class="com.myapp.auth.AuthenticationUserDetailsGetter" />

    <beans:bean id="passwordEncoder"
        class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
    </beans:bean>

    <global-method-security pre-post-annotations="enabled" />

    <!-- web services -->
    <http use-expressions="true" pattern="/rest/**"
        disable-url-rewriting="true" entry-point-ref="restAuthenticationEntryPoint">
        <custom-filter ref="restProcessingFilter" position="FORM_LOGIN_FILTER" />
        <intercept-url pattern="/rest/login" access="permitAll"/>
        <intercept-url pattern="/rest/**" access="isAuthenticated()" />
        <logout delete-cookies="JSESSIONID" />
    </http>

    <beans:bean id="restProcessingFilter" class="com.myapp.auth.RestUsernamePasswordAuthenticationFilter">
        <beans:property name="authenticationManager" ref="authenticationManager" />
        <beans:property name="filterProcessesUrl" value="/rest/login" />
    </beans:bean>

我将UsernamePasswordAuthenticationFilter 覆盖为

@Override
    public Authentication attemptAuthentication(HttpServletRequest request,
            HttpServletResponse response) {
        Authentication authentication = null;
        String username = request.getParameter("j_username");
        String password = request.getParameter("j_password");
        boolean valid = authService.authenticate(username, password);
        if (valid) {
            User user = updateLocalUserInfo(username);
            authentication = new UsernamePasswordAuthenticationToken(user,
                    null, AuthorityUtils.createAuthorityList("USER"));
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }
        return authentication;
    }

当我尝试使用上述身份验证时,它工作正常

RestClient restClient = new RestClient();
String result = restClient.login("hq", "a1234567"); // RestTemplate.postForObject

唯一剩下的是来自身份验证帖子的结果(atm,结果为空)。如何配置我的安全配置以检索一些结果?一个标志或会话 ID 就足够了。

【问题讨论】:

    标签: spring-security


    【解决方案1】:

    我认为这里最好的选择是AuthenticationSuccessHandler

    因为只有在身份验证成功时才会启动。 您可以生成某种 UUID 并直接在响应中设置它。我对 ReST Auth 使用了非常相似的方法,并且还没有遇到任何问题。

    详细实现指南请参考:https://stackoverflow.com/a/23930186/876142

    更新评论#1:

    您可以像任何正常的 ReST 请求一样获得响应。

    这就是我将我的令牌作为 JSON 发送回的方式

    String tokenJsonResponse = new ObjectMapper().writeValueAsString(authResponse);
    httpResponse.addHeader("Content-Type", "application/json");
    httpResponse.getWriter().print(tokenJsonResponse);
    

    假设你知道如何使用 RestTemplate,休息是微不足道的。

    【讨论】:

    • 如何获取RestClient中的回复?
    • 代码应该放在哪里?我是否需要执行额外的请求才能获得响应?
    • 不需要额外的请求,代码应该驻留在您的自定义 AuthenticationSuccessHandler 实现中。
    猜你喜欢
    • 2018-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 2014-12-25
    • 2015-02-11
    相关资源
    最近更新 更多