如何在 jhipster 中通过 OAuth2 成功登录后执行操作

2024-01-03

我想问一下通过 OAuth2 成功登录后如何执行操作以及如何根据某些先决条件否决登录。我尝试在 Google 上搜索并找到了一些链接,但我不确定如何在这个框架上执行此操作。我可能可以添加一些过滤器等,但想知道执行此操作的正确位置。

注意:AuditEvent 对我不起作用,因为每次 API 调用都会调用成功的审核。

Ref: http://blog.jdriven.com/2015/01/stateless-spring-security-part-3-jwt-social-authentication/ http://blog.jdriven.com/2015/01/stateless-spring-security-part-3-jwt-social-authentication/

我需要做的是:

  1. 成功登录后,在表中记录一些详细信息并向队列发送通知。除了成功登录之外,我还想在成功注销时执行一些操作,我知道我可以在这里执行一些操作:AjaxLogoutSuccessHandler。但是我无法找到类似的地方来成功登录。

  2. 在通过 OAuth2 登录之前,如果不满足特定条件,我可以抛出异常并不允许该用户。例如,如果用户来自特定的 IP 范围。我可以在哪里添加这个?

请引导我走向正确的方向。

Thanks


创建 TokenEndpointAuthenticationFilter 实现

CustomTokenEndpointAuthenticationFilter.java

public class CustomTokenEndpointAuthenticationFilter extends TokenEndpointAuthenticationFilter {

    public CustomTokenEndpointAuthenticationFilter(AuthenticationManager authenticationManager, OAuth2RequestFactory oAuth2RequestFactory) {

        super(authenticationManager, oAuth2RequestFactory);
    }

    @Override
    protected void onSuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, Authentication authResult) throws IOException {

                /* on successful authentication do stuff here */

    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
                /* before authentication check for condition if true then process to authenticate */
        if (!condition) {
            throw new AuthenticationServiceException("condition not satisfied");
        }
        super.doFilter(req, res, chain);
    }
}

Inside 授权服务器配置做出这些改变

@Configuration
@EnableAuthorizationServer
protected static class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

    @Inject
    private DataSource dataSource;

    @Inject
    private JHipsterProperties jHipsterProperties;

    @Bean
    public TokenStore tokenStore() {
        return new JdbcTokenStore(dataSource);
    }

    /* create OAuth2RequestFactory instance */
    private OAuth2RequestFactory oAuth2RequestFactory;

    @Inject
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
        throws Exception {
        /* assign value in OAuth2RequestFactory instance */
        oAuth2RequestFactory = endpoints.getOAuth2RequestFactory();
        endpoints
            .tokenStore(tokenStore())
            .authenticationManager(authenticationManager);
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
        /* register TokenEndpointAuthenticationFilter with oauthServer */
        oauthServer
            .allowFormAuthenticationForClients()
            .addTokenEndpointAuthenticationFilter(new CustomTokenEndpointAuthenticationFilter(authenticationManager, oAuth2RequestFactory));

    }

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
            .inMemory()
            .withClient(jHipsterProperties.getSecurity().getAuthentication().getOauth().getClientid())
            .scopes("read", "write")
            .authorities(AuthoritiesConstants.ADMIN, AuthoritiesConstants.USER)
            .authorizedGrantTypes("password", "refresh_token", "authorization_code", "implicit")
            .secret(jHipsterProperties.getSecurity().getAuthentication().getOauth().getSecret())
            .accessTokenValiditySeconds(jHipsterProperties.getSecurity().getAuthentication().getOauth().getTokenValidityInSeconds());
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 jhipster 中通过 OAuth2 成功登录后执行操作 的相关文章

随机推荐

  • 线程和执行器的正常关闭

    下面的代码试图实现这一点 该代码永远循环并检查是否有任何待处理的请求 如果有 它会创建一个新线程来处理请求并将其提交给执行器 所有线程完成后 它会休眠 60 秒 并再次检查待处理的请求 public static void main Str
  • 重写 Rust 中的总体特征实现

    对于我的游戏规则引擎 我有一个核心特征 称为Rule处理游戏回调 有两种类型Rules a BaseRule适用于游戏中的任何实体 并且CreatureRule仅适用于生物 我目前的代码结构如下 trait BaseRule
  • 具有多个倒计时器的 Recyclerview 导致闪烁

    我想显示我的每个细胞还剩下多少时间RecyclerView 为此 我为每个都使用了倒计时器 在每一行中我启动一个计数器并管理onTick 一切都按预期工作 我的每一行都有一个计时器滴答声 我的单元格也在更新 但我的单元格现在闪烁 当我滚动时
  • Python 动态装饰器 - 为什么有这么多换行?

    所以我对 Python 装饰器还是有点陌生 我以前用过它们 但我从未制作过自己的装饰器 我正在阅读本教程 http www siafoo net article 68 run time tranformations 那个特定的段落 我似乎不
  • 在 Swift 中使用块给出错误“变量在其自己的初始值内使用”

    这是我在 obj c 中的代码 block NSString requestReference self operation method url url parameters parameters headers headers succ
  • 为什么 SQLException 没有捕获 SQLiteExcpetion?

    我的一个 Android 应用程序中有一些代码可以捕获 SQLException 但我最近发现它没有捕获 SQLiteException 显然 SQLiteException 是 SQLException 的子级 那么为什么它没有被捕获呢
  • 为什么内联元素只有上边框溢出?为什么不离开边框?

    我有一个 div 容器 固定宽度和高度 其中包含一个边框为 10px 的内联元素 内联元素的上边框溢出 为什么不离开边框 下面是我的代码 container width 100px height 100px border 1px solid
  • Java 中 Math.max(a,b) 或 (a>b)?a:b 更快吗?

    Java 中哪一个更快 为什么 Math max a b a gt b a b 这是在采访中被问到的 Here http grepcode com file repository grepcode com java root jdk ope
  • RichTextBox 中的 HTML 格式

    我一直在使用从 XML 文件获取的 HTML 字符串 我正在尝试找出一种方法来在带有格式的富文本框中显示这些字符串 所以 例如 p This is a strong HTML strong string from the em XML em
  • 如何在C#中显示文件夹中所有文件的统计信息[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 输出必须是这样的 文件位于 C Windows 文件总数 49 所有文件的总大小 7121424 字节 最大文件 explorer exe
  • 未提供所需的模拟级别,或者提供的模拟级别无效

    我在 WCF 服务和模拟方面遇到了一些问题 我将其提炼为下面的一个简单方法 WCF 服务当前在 exe 中自行托管 异常消息是 未提供所需的模拟级别 或者提供的模拟级别无效 检查何时抛出错误 身份模拟级别设置为委派 如我的客户端上指定的那样
  • Python“sys.getsizeof”在从列表/字典中删除项目后报告相同的大小?

    我注意到 当使用 sys getsizeof 检查列表和字典的大小时 会发生一些有趣的事情 i have a 1 2 3 4 5 大小为 56 字节 空列表的大小为 36 所以这是有道理的 因为 20 5 4 但是 在我删除列表中的所有项目
  • 如何在NestedScrollView中实现ConstraintLayout

    ConstraintLayout is behaving inconsistently if a TextView has very fewer texts than other TextView wrapped in horizontal
  • 用于 Java 开发的最佳 Eclipse 版本 [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 用于 Java 开发的 Eclipse 有许多版本 我正在尝试 MyEclipse 其开箱即用的体验给我留下了深刻的印象 在我适应它之前 我想知
  • 处理 ASP.NET Core 3.1 中未处理的异常

    在 ASP NET Core 3 1 中添加了一项功能 可以将未处理的异常传递到 ILogger 的实例 如下所示 登录 NET Core 和 ASP NET Core https learn microsoft com en us asp
  • 异质平等的一致性

    我正在尝试使用异构相等来证明涉及此索引数据类型的语句 data Counter Set where cut i j Counter suc i j 我能够使用以下方式编写我的证明Relation Binary HeterogenousEqu
  • 如何在浏览器中播放MKV文件?

    我有一个视频文件MKV格式 我想在浏览器中播放该文件而不进行转换 如何在浏览器中播放该文件格式
  • 简洁的架构:在哪里进行 API 调用

    我目前正在创建一个微服务项目 在其中实现 Bob Martin 创造的清洁架构模式 虽然我的代码运行良好 但我对干净的架构模式有疑问 特别是接口和 use cases 层 该应用程序是我正在开发的一个小型电子商务 POC 话虽如此 由于它正
  • 从 Python 字典批量更新 PostgreSQL

    在现有的 PostgreSQL 表中 我想UPDATE几个现有列 其值来自字典查找 请参见下面的字典 有点像这里描述的不错的博文 http tapoueh org blog 2013 03 15 batch update 但是 我不知道如何
  • 如何在 jhipster 中通过 OAuth2 成功登录后执行操作

    我想问一下通过 OAuth2 成功登录后如何执行操作以及如何根据某些先决条件否决登录 我尝试在 Google 上搜索并找到了一些链接 但我不确定如何在这个框架上执行此操作 我可能可以添加一些过滤器等 但想知道执行此操作的正确位置 注意 Au