将基于令牌的安全性集成到现有的 Spring Security Web 应用程序中

2024-03-14

我正在设计一个 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 服务。 谢谢。

当用户第一次想要访问 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 服务

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将基于令牌的安全性集成到现有的 Spring Security Web 应用程序中 的相关文章

随机推荐

  • 为什么“git submodule update”会跳过子模块?

    我有一个带有单个子模块的 git 存储库sub x 该子模块不包含其自己的任何子模块 在超级项目的存储库中 输出git status显示以下 未暂存的 修改 modified sub x new commits 如果我现在跑步 git su
  • 鼠标交互的设计模式

    我需要一些关于什么是通用鼠标的 理想 设计模式的意见 相互作用 这里是简化的问题 我有一个小型 3d 程序 QT 和 openGL 并且 我使用鼠标进行交互 每一次互动通常不仅仅是一次 单个函数调用 主要由最多 3 个函数调用 启动 执行
  • EmailAddressAttribute 不需要

    我有一个 EmailAddress 来自 net 4 5 的模型属性上的 DataAnnotation 它返回一个 电子邮件字段不是有效的电子邮件地址 当 Email 属性为空时 验证期间出错 虽然这在技术上是正确的 但我预计这个空值只能用
  • Rails sort_by 方法有两个字段,一个按升序排序,一个按降序排序

    我想渲染一个按分数排序的部分 然后按名称排序 如果多个玩家具有相同的分数 现在我正在使用这个 这是可行的 但它按升序对分数进行排序 而我想按降序对它们进行排序 如何翻转分数的排序顺序 但不翻转名称的排序顺序 我仍然想按升序排序 Thanks
  • 在 JPA 2.0 JPQL 中,当返回一个 NEW 对象时,如何使用 FETCH JOIN?

    我的一位同事有以下 显然无效的 JPQL 查询 SELECT NEW com foobar jpa DonationAllocationDTOEntity a id a campaign a campAppeal a campDivisio
  • 从 Excel VBA 运行 Powershell 命令(非脚本)

    我已经搜索过 并且可以找到大量从 VBA 运行 PowerShell 脚本的示例 但我找不到任何仅运行简单命令的示例 例如 这有效 Dim retval As Variant retval Shell PowerShell C MyScri
  • 打印文件中的第一个和最后一个匹配项

    对于以下问题是否有更清洁的解决方案 grep INFO messages head 1 grep INFO messages tail 1 INFO 或消息的长度是随机的 Try grep INFO messages sed n 1p p
  • 将 async/await 与 forEach 循环结合使用

    使用有什么问题吗async await in a forEach环形 我正在尝试循环遍历文件数组并await关于每个文件的内容 import fs from fs promise async function printFiles cons
  • 您是否积极管理技术债务? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 你是否主动管理技术债务 http forums construx com blogs stevemcc archive 2007 11
  • Bash 读取忽略前导空格

    我有文件a txt包含以下内容 aaa bbb 当我执行以下脚本时 while read line do echo line done lt a txt gt b txt 生成的b txt包含以下内容 aaa bbb 可以看出 行的前导空格
  • 如何使用 Perl 将客户端从一个 CGI 页面重定向到另一页面?

    我的问题如下 密码被识别为有效后 我需要重定向到main cgi但我收到的消息是 Status 302 Found Location http localhost cgi bin Main cgi 我知道这样做的原因是我在之后写下这份声明C
  • 如果我使用圆形图像,图像不会显示在 imageView 中

    我有一个表视图 我需要在节标题中显示author img 在 ViewForSectionHeader 方法中 我想让图像成为圆形 但如果我这样做 无论在模拟器中还是在真实设备中 图像都根本不会显示 如果我删除代码 uiimageview
  • 如何从 SELECT 语句输出进度消息?

    我有一个 SQL 脚本 我想在运行时输出进度消息 让它在 SQL 语句之间输出消息很容易 但是我有一些运行时间很长的 INSERT INTO SELECT 有没有办法让 select 语句随时输出消息 例如每 1000 行或每 5 秒输出一
  • Heroku:puppeteer chrome:加载共享库时出错:libX11-xcb.so.1

    使用部署应用程序时出现以下错误react snap到赫罗库 puppeteer local chromium linux 686378 chrome linux chrome error while loading shared libra
  • Clojure 映射限制和一致性

    我想知道 考虑到 Clojure 使用 32 位哈希来实现其映射 因此 Clojure 映射是否有 2 32 1 个键的限制 如果这不是真的 它如何管理冲突 以及它的哈希是否实施是持续的 http en wikipedia org wiki
  • Prolog 的良好初学者材料 [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • MySQL Workbench 6.1 - 没有插入行的选项?

    MySQL Workbench 5 2 版提供了一个选项 可以通过右键单击表列表中的表名称 以图形方式向表中添加行 MySQL Workbench 6 1 中确实缺少此选项还是隐藏了 在 Ubuntu 13 10 上 MySQL 工作台 6
  • 如何将 byte[] 序列化为简单的 JSON 数组而不是 JSON.net 中的 base64?

    我使用 JSON net 在 C 和 JavaScript 之间序列化一些对象 JSON 数据通过 WebSocket 在 NET 和浏览器应用程序之间传输 数据结构中有一些byte 字段 我希望这些字段作为Array在 JavaScrip
  • 脚手架控制器 vs2015 时错误键已存在于表中

    我正在尝试使用 VS2015 遵循 Professional MVC 4 中的音乐商店示例 我在搭建音乐商店控制器时遇到问题 每次我尝试创建控制器时 都会弹出一个错误窗口 其中唯一的信息是 运行所选代码生成器时出错 表中已存在密钥 我已经搜
  • 将基于令牌的安全性集成到现有的 Spring Security Web 应用程序中

    我正在设计一个 RESTful Web 服务 用户需要在正确的身份验证后才能访问该服务 我已经使用 Spring Security 3 0 为我的应用程序开发了安全性 现在我想集成TokenBasedAuthentication 但我坚持在