微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我已按照 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";
    }
}

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

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

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

但他们都没有帮助我。

解决方法

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

application.yml 而不是 application.template.yml

问题消失了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。