如何解决此问题:LoginController 中构造函数的参数 0 需要找不到类型为“OktaOAuth2Properties”的 bean

Posted

技术标签:

【中文标题】如何解决此问题:LoginController 中构造函数的参数 0 需要找不到类型为“OktaOAuth2Properties”的 bean【英文标题】:How to fix the issue: Parameter 0 of constructor in LoginController required a bean of type 'OktaOAuth2Properties' that could not be found 【发布时间】:2021-09-03 03:01:49 【问题描述】:

我已关注 tutorial,使用 Java 11 将社交登录添加到 Spring Boot 应用程序。我遇到的问题是:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.okta.spring.example.controllers.LoginController required a bean of type 'com.okta.spring.boot.oauth.config.OktaOAuth2Properties' that could not be found.


Action:

Consider defining a bean of type 'com.okta.spring.boot.oauth.config.OktaOAuth2Properties' in your configuration.

项目结构是:

src
   |-- main
   |   |-- java
   |   |   |-- com
   |   |   |   |-- okta
   |   |   |   |   |-- spring
   |   |   |   |   |   |-- example
   |   |   |   |   |   |   |-- HostedLoginCodeFlowExampleApplication.java
   |   |   |   |   |   |   |-- controllers
   |   |   |   |   |   |   |   |-- HomeController.java
   |   |   |   |   |   |   |   |-- LoginController.java
   |   |   |   |   |   |   |   |-- UserDetailsController.java
   |   |-- resources
   |   |   |-- application.template.yml
   |   |   |-- static
   |   |   |   |-- img
   |   |   |   |   |-- icon-spring-cloud.svg
   |   |   |-- templates
   |   |   |   |-- 403.html
   |   |   |   |-- head.html
   |   |   |   |-- home.html
   |   |   |   |-- login.html
   |   |   |   |-- menu.html
   |   |   |   |-- userProfile.html
   |-- test
   |   |-- resources
   |   |   |-- logback.xml
   |   |   |-- package.json
   |   |   |-- testRunner.yml

LoginController 就像:

@Controller
public class LoginController 

    private static final String STATE = "state";
    private static final String NONCE = "nonce";
    private static final String SCOPES = "scopes";
    private static final String OKTA_BASE_URL = "oktaBaseUrl";
    private static final String OKTA_CLIENT_ID = "oktaClientId";
    private static final String REDIRECT_URI = "redirectUri";
    private static final String ISSUER_URI = "issuerUri";

    private final OktaOAuth2Properties oktaOAuth2Properties;

    public LoginController(OktaOAuth2Properties oktaOAuth2Properties) 
        this.oktaOAuth2Properties = oktaOAuth2Properties;
    

    @GetMapping(value = "/signin")
    public ModelAndView login(HttpServletRequest request,
                              @RequestParam(name = "state", required = false) String state,
                              @RequestParam(name = "nonce") String nonce) throws MalformedURLException 

        // if we don't have the state parameter redirect
        if (state == null) 
            return new ModelAndView("redirect:" + oktaOAuth2Properties.getRedirectUri());
        

        String issuer = oktaOAuth2Properties.getIssuer();
        // the widget needs the base url, just grab the root of the issuer
        String orgUrl = new URL(new URL(issuer), "/").toString();

        ModelAndView mav = new ModelAndView("login");
        mav.addObject(STATE, state);
        mav.addObject(NONCE, nonce);
        mav.addObject(SCOPES, oktaOAuth2Properties.getScopes());
        mav.addObject(OKTA_BASE_URL, orgUrl);
        mav.addObject(OKTA_CLIENT_ID, oktaOAuth2Properties.getClientId());
        // from ClientRegistration.redirectUriTemplate, if the template is change you must update this
        mav.addObject(REDIRECT_URI,
            request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +
            request.getContextPath() + "/authorization-code/callback"
        );
        mav.addObject(ISSUER_URI, issuer);

        return mav;
    

    @GetMapping("/post-logout")
    public String logout() 
        return "logout";
    

    @GetMapping("/403")
    public String error403() 
        return "403";
    

问题是如果所有文件都按照每个步骤进行配置,如何解决它。

我检查了以下不同的答案:

    Consider defining a bean of type 'package' in your configuration Parameter 0 of constructor in … Spring Boot Parameter 0 of constructor in required a bean of type 'java.lang.String' that could not be found

但他们都没有帮助我。

【问题讨论】:

【参考方案1】:

问题在于文件命名。当我改名yml:

application.yml 而不是application.template.yml

问题消失了。

【讨论】:

以上是关于如何解决此问题:LoginController 中构造函数的参数 0 需要找不到类型为“OktaOAuth2Properties”的 bean的主要内容,如果未能解决你的问题,请参考以下文章

LoginController未登录

退出后如何防止显示主页

从 Grails 插件覆盖控制器

在 Laravel 中登录多个角色后重定向

Grails Ajax 登录

如何在 Windows 10 中解决此问题?