当前位置: 首页 > 知识库问答 >
问题:

带表单登录的Spring Boot Security OAuth2

程成天
2023-03-14

我正在学习Spring Boot Security入门第五部分,以保护我的RESTful微服务。

我打算实现的简单流程是:-

>

  • 如果未经身份验证,用户将被重定向到一个自定义登录页,地址是'/login'。

    用户提供他的凭据。

    上面提到的链接中的入门指南使用了在。properties或。yml文件中配置的基本Auth和虚拟用户。

    这是我尝试配置的方式:-

    @Configuration
    @EnableAuthorizationServer
    public class OAuth2Config extends AuthorizationServerConfigurerAdapter {
    
        @Autowired
        private AuthenticationManager authenticationManager;
    
        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints.authenticationManager(authenticationManager);
        }
    
        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.inMemory().withClient("acme").secret("acmesecret")
                    .authorizedGrantTypes("authorization_code", "refresh_token", "password").scopes("openid")
                    .accessTokenValiditySeconds(3600);
        }
    
        @Override
        public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
            oauthServer.tokenKeyAccess("isAnonymous()").checkTokenAccess("isAnonymous()")
                    .allowFormAuthenticationForClients();
        }
    
    }
    
    
    
    @Configuration
    @Import({ OptoSoftSecurityServiceConfig.class })
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
        @Autowired
        private UserDetailsService userDetailsService; // backed by MongoDB
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(userDetailsService);
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.httpBasic().disable().formLogin();// disabled basic auth and configured to use dafault Spring Security form login.
        }
    }
    

    单击授权endpoint会将我重定向到“http://localhost:9999/uaa/login”,错误消息如下:-

    <oauth>
    <error_description>
    Full authentication is required to access this resource
    </error_description>
    <error>unauthorized</error>
    </oauth>
    

    问题

    >

  • 如何将授权服务器配置为使用UserDetailsService而不是静态用户,并使用表单登录而不是基本Auth。

    如何在使用“authorization_code”作为授权类型时配置自动审批?

    /oauth/authorizeendpoint是否必须受基本auth的保护?为什么“需要完全身份验证”才能访问/oauth/authorize“endpoint。我相信我们不知道谁是这个endpoint之前的用户。只有使用表单登录后的有效凭据对用户进行身份验证后,才能识别该用户。

  • 共有1个答案

    曾嘉瑞
    2023-03-14

    终于让它起作用了。上面提到的博客中的git repo已经配置了这个东西。结果很直接。

    这对我很有效(我还将自动审批配置为true):-

    **
     * @author kumar
     *
     */
    @SpringBootApplication
    public class AuthenticationServerApplication {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            SpringApplication.run(AuthenticationServerApplication.class, args);
    
        }
    
        @Configuration
        protected static class LoginConfig extends WebSecurityConfigurerAdapter {
    
            @Autowired
            private AuthenticationManager authenticationManager;
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.formLogin().permitAll().and().authorizeRequests().anyRequest().authenticated();//.and().userDetailsService(yourCustomerUserDetailsService);
            }
    
            @Override
            protected void configure(AuthenticationManagerBuilder auth) throws Exception {
                auth.parentAuthenticationManager(authenticationManager);
            }
        }
    
        @Configuration
        @EnableAuthorizationServer
        protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
    
            @Autowired
            private AuthenticationManager authenticationManager;
    
            @Override
            public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
                endpoints.authenticationManager(authenticationManager);
            }
    
            @Override
            public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
                clients.inMemory().withClient("acme").secret("acmesecret")
                        .authorizedGrantTypes("authorization_code", "refresh_token", "password").scopes("openid")
                        .autoApprove(true);
            }
    
            @Override
            public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
                oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
            }
    
        }
    
    }
    

    申请。yml:-

      security:
          user:
            password: password
        server:
          port: 9999
          context-path: /uaa
    
     类似资料:
    • 我尝试使用这个简单的代码登录,但在我点击登录按钮后,else选项“无效用户名或密码!”总是出现,即使用户名和密码是匹配的 控制器:

    • 我是Symfony的新手,我正在尝试为我的应用程序创建一个access登录表单,但当我尝试登录时,它只会一次又一次地将我重定向到表单登录。在此之前,我使用了HTTP基本表单,它工作得很好,但现在我尝试使用登录表单,这是不可能的。 这是我的代码: 用户类(带教义): 安全Controller.php: Security.yml login.html.twig 有什么想法吗?谢谢

    • 我在OS X上使用selenium webdriver编写了一个python 2.7脚本来登录Yahoo fantasy sports并自动执行一些操作。 该脚本与webDriver、Firefox和ChromeDriver都能很好我最近开始使用PhantomJS(GhostDriver),我发现我无法让PhantomJS Selenium驱动程序(GhostDriver)登录到雅虎登录表单。 请

    • 我将springBoot与JOOQ一起使用,并希望记录生成的SQL。 我将slf4J添加到maven依赖项和log4j.xml中,就像在JOOQ文档中一样(http://www.JOOQ.org/doc/latest/manual/sql-execution/logging/)。但是当jooq执行一些查询时,我在控制台中看不到任何日志。

    • 我一直在关注symfony网站上的教程(http://symfony.com/doc/current/security.html)为我的Symfony 3.1项目添加登录名。它可以与HTTP基本身份验证一起工作,但是当我使用这里的教程添加form_登录时(http://symfony.com/doc/current/security/form_login_setup.html)它会一直重定向到登录

    • 问题内容: 我正在忙于开发一个网站,该网站的右上角有一个登录框。我希望用户能够在不刷新的情况下登录同一页面。 好的,我已经开始工作了,但是我仍在努力登录后的过程,如下所示: (HTML) (PHP) (我的jQuery) (isLoggedIn.php) (isNotLoggedIn.php) 登录过程可以正常工作,但是注销混乱。当我单击isLoggedIn.php中的Logout链接时,它注销了