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.

2
  • stackoverflow.com/a/42843551/4725592 Commented May 7, 2018 at 10:07
  • @HarshitShrivastava I just needed to convert 401 to 404 just for this particular method. How can I use reflections to detect class and method and then send ? Commented May 7, 2018 at 10:11

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I cannot have the @ConditionalOnProperty annotation on a class level. I just need it for the method
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.