Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Controller method from Java

I would like to invoke a Controller's method via a Java class, so I can return a specific view. In this case I have a short list of IDs; if the current user's ID is not in that list, redirect to a view invalidUser.

I can do it on the client-side with either Ajax or a button: onclick="location.href='/invalidUser'

But I am not clear on how I can call the ViewsController's invalidUser() method from a Java class.

How can I do that with Java in the invalidUserRedirect() method? I was thinking to get the base URL from the HttpServletRequest as shown here: Get Root/Base Url In Spring MVC and then make an http call to baseUrl + "/invalidUser" but that does not seem like the right approach.

AuthService:

@Service
public class AuthService {

  public void invalidUserRedirect(HttpServletRequest request) {
    // Make call to invalidUser() in ViewsController
  }
}

Views Controller:

@Controller
public class ViewsController {
  @RequestMapping(value = "/invalidUser", method = {RequestMethod.GET})
  public String invalidUser() {
    return "invalid";
  }

}

like image 391
Boris Avatar asked Apr 10 '26 05:04

Boris


2 Answers

Controller classes are called from your browser. You shouldn't call a Controller method from your service class. Your controller method should call invoke your service class

like image 158
nLee Avatar answered Apr 12 '26 19:04

nLee


Controller class generally used for redirecting flow of application based on your business logic. Most of all the method in controller are marked with @RequestMapping annotation, even though you are able to call the controller method from the service, it will not able to fulfill the purpose as the return type of Controller is resulting specific view. You have to write implementation of AuthenticationFailureHandler to achieve the functionality. This is easily achievable by spring security

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

@Component
public class MyAuthenticationFailureHandler  implements AuthenticationFailureHandler{

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
    AuthenticationException exception) throws IOException, ServletException {
    request.setAttribute("param", "invaliduser");
    response.sendRedirect("/domain/?error");
} }

Call this class in the Security class

@Autowired
 MyAuthenticationFailureHandler failureHandler;

  @Override
  protected void configure(HttpSecurity http) throws Exception {
  http.formLogin()
                .loginPage(LOGIN_URL)
                .failureUrl(LOGIN_URL + "?error").permitAll()
                .authenticationDetailsSource(authDetailsSource)
                .successHandler(successHandler)
                .failureHandler(failureHandler);
      }
like image 25
Mohit Sharma Avatar answered Apr 12 '26 18:04

Mohit Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!