0

I have a piece of code for showing login page depending on whether user is logged in or not

@PropertySource(value = "classpath:securityConfig.properties", ignoreResourceNotFound = true)
@Controller
public class IndexController {
    private static final String LOGIN_PAGE           = "login";
    private static final String HOME_PAGE            = "home";
    private static final String LOBBY_PAGE           = "lobby";
    private static final String FORGOT_USER_PAGE     = "forgotUserName";
    private static final String FORGOT_PASSWORD_PAGE = "forgotPassWord";

    @Value("${auth.mode:fixed}")
    private String authenticationMode;

    public String getAuthenticationMode(){
         return this.authenticationMode;
    }

    @PreAuthorize("isAnonymous() AND this.getAuthenticationMode().equals(\"fixed\")")
    @RequestMapping(method = RequestMethod.GET, value = { "/login" })
    public String getIndexPage() {
        return LOGIN_PAGE;
    }

}

The @PreAuthorize annotation throws HTTP 401 if the condition isn't satisfied. How do I throw HTTP 404 instead? I just want to throw 404 for this particular method ONLY.

Prateek Narendra
  • 1,837
  • 5
  • 38
  • 67

1 Answers1

0

If you simply don't define the controller then you'll receive a 404 Not Found. This can be achieved by using a @ConditionalOnProperty annotation on the controller.

See my previous answer https://stackoverflow.com/a/50211750/134894

ptomli
  • 11,730
  • 4
  • 40
  • 68
  • I cannot have the `@ConditionalOnProperty` annotation on a class level. I just need it for the method – Prateek Narendra May 07 '18 at 12:36
  • Why not? If you say that because you want to have other handlers, then simply have the login handler in a distinct controller class from the other handlers that you do want to include. – ptomli May 07 '18 at 14:06