Spring Oauth2隐式流程

2023-11-29

致力于使用 Spring 实现 Oauth2。我想实现隐式工作流程:

我的配置文件:

@Configuration
@EnableAutoConfiguration
@RestController
public class App {

    @Autowired
    private DataSource dataSource;

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @RequestMapping("/")
    public String home() {
        return "Hello World";
    }

    @Configuration
    @EnableResourceServer
    protected static class ResourceServer extends ResourceServerConfigurerAdapter {

        @Autowired
        private TokenStore tokenStore;

        @Override
        public void configure(ResourceServerSecurityConfigurer resources)
                throws Exception {
            resources.tokenStore(tokenStore);
        }

        @Override
        public void configure(HttpSecurity http) throws Exception {
            // @formatter:off
        http.authorizeRequests().antMatchers("/oauth/token").authenticated()
                .and()
                .authorizeRequests().anyRequest().permitAll()
                .and()
                .formLogin().loginPage("/login").permitAll()
                .and()
                .csrf().disable();
        }

    }

    @Configuration
    @EnableAuthorizationServer
    protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {

        @Autowired
        private AuthenticationManager auth;

        private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

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

        @Bean
        protected AuthorizationCodeServices authorizationCodeServices() {
            return new JdbcAuthorizationCodeServices(DBConnector.dataSource);
        }

        @Override
        public void configure(AuthorizationServerSecurityConfigurer security)
                throws Exception {
            security.passwordEncoder(passwordEncoder);
        }

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints)
                throws Exception {
            endpoints.authorizationCodeServices(authorizationCodeServices())
                    .authenticationManager(auth).tokenStore(tokenStore())
                    .approvalStoreDisabled();            
        }

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            // @formatter:off
            clients.jdbc(DBConnector.dataSource)
                    .passwordEncoder(passwordEncoder)
                    .withClient("my-trusted-client")
                    .secret("test")
                    .authorizedGrantTypes("password", "authorization_code",
                            "refresh_token", "implicit")
                    .authorities("ROLE_CLIENT", "ROLE_TRUSTED_CLIENT")
                    .scopes("read", "write", "trust")
                    .resourceIds("oauth2-resource")
                    .accessTokenValiditySeconds(0);

            // @formatter:on
        }

    }

    @Autowired
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        // @formatter:off 
        auth.jdbcAuthentication().dataSource(DBConnector.dataSource).withUser("dave")
                .password("secret").roles("USER");

        // @formatter:on
    }

}

到目前为止,这是有效的。数据库中还会生成一个用户。

问题如下。当我尝试执行以下请求时:

我总是收到一个弹出窗口(身份验证),要求我输入用户名和密码。但无论我进入那里,我都不会经过。那么哪里出了问题呢?

我希望当我调用这个 url 时,我可以取回我的 access_token。


在隐式流的情况下,所有令牌都将通过授权 url 而不是令牌 url 生成。所以你应该使用隐式响应类型点击 ../oauth/authorize 端点。 IE

../oauth/authorize?response_type=implicit&client_id=trusted_client&redirect_uri=<redirect-uri-of-client-application>.

您收到用户名密码弹出窗口,因为令牌端点已经通过 spring 的 BasicAuthenticationFilter 进行保护,并且它期望您将 client_id 作为用户名传递,将 client_secret 作为密码传递。您需要保护授权端点,而不是令牌端点,因此您的端点安全配置也要按照给定的方式进行...

 @Override
        public void configure(HttpSecurity http) throws Exception {
            // @formatter:off
        http.authorizeRequests().antMatchers("/oauth/authorize").authenticated()
                .and()
                .authorizeRequests().anyRequest().permitAll()
                .and()
                .formLogin().loginPage("/login").permitAll()
                .and()
                .csrf().disable();
        }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring Oauth2隐式流程 的相关文章

随机推荐

  • 对 URL 进行编码/解码

    在 Go 中编码和解码整个 URL 的推荐方法是什么 我知道这些方法url QueryEscape and url QueryUnescape 但它们似乎并不正是我正在寻找的 具体来说 我正在寻找像 JavaScript 这样的方法enco
  • 如何在 Scala 3 中证明 `Tuple.Map[H *: T, F] =:= (F[H] *: Tuple.Map[T, F])`

    我正在尝试编写一个包含元组类型给定实例的特征 是的 我知道summonAll存在 trait TupleInstances C T lt Tuple val instances Tuple Map T C given C TupleInst
  • 无法在 codeigniter 3 中使用 SMTP gmail 配置发送电子邮件

    下面是我的代码 我参考了堆栈溢出和 codeigniter 用户指南中的所有示例 我仍然无法解决这个问题 public function send config protocol smtp config smtp crypto ssl co
  • Access/Excel VBA - 时间延迟

    Note 刷新 Excel 中链接到 Access 数据库的表 Excel 中的表需要按顺序刷新 例如 Test Sheet1 Test Sheet2 Test Sheet3 Excel 文件由多个用户访问 问题 在 Access vba
  • 查看更多和查看更少按钮

    下面的脚本每次单击按钮时都会显示 4 个项目 我需要的是在单击后更改按钮的文本以 显示更多 然后在显示所有项目时更改为 显示更少 我尝试添加这个 if nowShowing gt numInList partners button a to
  • ngRepeat 按深层属性过滤

    如果我有一个以对象作为属性值的复杂对象 如何按嵌套属性之一进行过滤 这可以通过 OOB ng repeat 过滤器来完成吗 Data Name John Smith Manager id 123 Name Bill Lumburg ngRe
  • 使用 href onclick 更新 div 而不重新加载页面?

    我用这个现有的问题来帮助我 HTML 更改 更新页面内容而不刷新 重新加载页面 template comparison php 文件从 header php 代码中获取代码 但实际的 获取代码 并未显示 否则 模板页面将没有标题 templ
  • Play 位置服务 getLastLocation 返回 null

    我正在尝试聆听位置变化 但有时onLocationChanged回调永远不会被调用并且getLastLocation回报null 而谷歌地图始终运行完美 Note 如果我重新启动设备 定位服务将仅工作约 2 天 之后我的应用程序和SDK 示
  • 即使提供了参数,过程仍需要参数

    我看到其他一些人也面临着类似的问题 我已阅读并检查了标题为的问题过程需要未提供的参数 我认为这可以解决我的问题 但我错了 我确实检查了那里建议的步骤 但没有成功 这是我的代码 oOleDbCommand CommandText usp Pe
  • 如何在 iPhone 上以卡拉 OK 风格显示歌词?

    我目前正在创建一个播放音乐的应用程序 我想添加一个功能 在播放音乐时显示音乐歌词 并标记文本的当前位置以匹配歌曲中的当前位置 弹跳球效果 就像您在播放歌曲时在每个卡拉 OK 屏幕上看到的一样 我一直在考虑扩展我的咖啡馆文件 添加 字符串块
  • 实体框架查找方法无法正常工作

    我有名为 课程 学生 和 教师 的课程 如下所示 public class Course Key DatabaseGenerated DatabaseGenerationOption Identity public Guid CourseI
  • 如何将 Bluebird 与 Angular 结合使用?

    我尝试使用 Angular 和 Bluebird 承诺 HTML div name also div JS javascript var app angular module HelloApp app controller HomeCont
  • 通过 Powershell 更改 Chrome 设置

    我想编写一个脚本来更改 Chrome 中的默认页面缩放 但我不知道这些选项存储在哪里 我想我必须找到一个合适的选项文本文件 解析它 然后使用 powershell 进行文本替换才能应用更改 每次将笔记本电脑连接到外部显示器时 我都需要执行此
  • 非拉丁字符和哎哟

    我正在了解 Cake PHP 它发现了一个关于 PHP MySQL 字符集内容最佳实践的一般问题 我希望可以在这里得到解答 我的 练习 系统包含一个 mysql 电影表 该列表源自 Excel 工作表 该工作表导出为 CSV 并通过 php
  • 亚马逊 Linux 上的 uwsgi 新贵

    我按照本教程创建了一个 uwsgi 文件https uwsgi readthedocs org en latest Upstart html在亚马逊Linux上 虽然它似乎没有运行 因为 Nginx 只是说网关不好 如果我运行 etc in
  • JLabel:异步加载 HTML 图像

    A JLabel允许 HTML 内容 其中可以包含图像 String html img src JLabel label new JLabel html 请注意 我使用JLabel用于渲染图像JXTreeTable 因此更新文本JLabel
  • 如何从 C# 应用程序中将焦点设置到桌面

    Winforms 应用程序 Net 3 5 我需要将焦点从 C 应用程序设置到用户桌面 几乎就像模拟鼠标在桌面上单击一样 有人可以告诉我如何用 C 做到这一点吗 我只想将焦点设置在桌面上 以便焦点不再位于我的应用程序上 但我想在我的应用程序
  • 在 Perl 中打印字符串

    有没有一种简单的方法 也许使用子例程 在 Perl 中打印字符串而不转义每个特殊字符 这就是我想做的 print DELIMITER i DELIMITER 显然 如果我可以使用字符串而不是特殊字符作为分隔符 那就太好了 佩尔多克 佩洛普
  • 多线程并发访问和全局互斥

    OpenSSL 常见问题解答指出它可以在线程应用程序中使用 1 OpenSSL是线程安全的吗 如果应用程序设置了线程回调函数 答案是肯定的 此回调函数引用全局 SSL 锁 因此如果您有 2 个 ssl 连接运行 它们都将使用此全局锁 然而
  • Spring Oauth2隐式流程

    致力于使用 Spring 实现 Oauth2 我想实现隐式工作流程 我的配置文件 Configuration EnableAutoConfiguration RestController public class App Autowired