Search code examples
javaspring-bootoktaokta-api

How to fix the issue: Parameter 0 of constructor in LoginController required a bean of type 'OktaOAuth2Properties' that could not be found


I've followed tutorial to add social login to Spring Boot app using Java 11. The issue I've encountered was:

***************************
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.

Project structure was:

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 was like:

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

The problem was how to fix it if all files were configured according to every step.

I've checked different answers from:

  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

but none of them helped me.


Solution

  • The problem was in file naming. When I changed the name of yml:

    application.yml instead of application.template.yml

    the issue was gone.