如何解决此问题: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

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

***************************
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";
    }
}

问题是如果所有文件都按步骤配置了怎么解决。

我检查了不同的答案:

但其中 none 帮助了我。

问题出在文件命名上。当我更改 yml 的名称时:

application.yml 而不是 application.template.yml

问题已解决。