-
Notifications
You must be signed in to change notification settings - Fork 77
Print the User Information in catalina files while the log level is INFO #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| public void commence(HttpServletRequest request, HttpServletResponse response, | ||
| AuthenticationException authException) throws IOException, ServletException { | ||
| if (authException instanceof BadCredentialsException) { | ||
| LOGGER.info("Bad Credentials {}", HttpStatus.UNAUTHORIZED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use warning instead of info.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed the info to warn.
| public void commence(HttpServletRequest request, HttpServletResponse response, | ||
| AuthenticationException authException) throws IOException, ServletException { | ||
| if (authException instanceof BadCredentialsException) { | ||
| LOGGER.info("Bad Credentials {}", HttpStatus.UNAUTHORIZED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we get more details regarding the exception, i.e. authException.getMessage()? It may be helpful for an admin to investigate what really went wrong (username/password or maybe something else).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested with adding authException.getMessage(), it's just returning message like Bad Credentials. below is the log line Bad Credentials 401 UNAUTHORIZED Bad credentials. So, not modifying the log message
| UserDetails userDetails = (UserDetails) authentication.getPrincipal(); | ||
| // Get the username of the authenticated user | ||
| String username = userDetails.getUsername(); | ||
| log.info("Authentication Successful for user {} ", username); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works fine, but I found 2 issues:
- I think "User name: {}" is enough. Non-authenticated user cannot get here.
- The same log should be done for generatreAndPublish(). Please, don't copy the code, create a function instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified the code.
| this.restTemplate = restTemplate; | ||
| } | ||
|
|
||
| public void getUserName() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name getUserName() indicates that user name is returned and it might be confusing. Please, rename to something more appropriate, e.g. logUserName().
| UserDetails userDetails = (UserDetails) authentication.getPrincipal(); | ||
| // Get the username of the authenticated user | ||
| String username = userDetails.getUsername(); | ||
| log.info("User name {} ", username); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, add a colon ':' to separate the username from a text: "User name: {}"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works fine. User name properly loggged for /generateAndPublish and /producer/msg endpoints.
Applicable Issues
Description of the Change
Made the Necessary changes to print the user information while the log level is info and created a class which return unauthorized error when credentials are wrong.Alternate Designs
Benefits
Possible Drawbacks
Sign-off
Developer's Certificate of Origin 1.1
By making a contribution to this project, I certify that:
(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or
(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or
(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.
(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.
Signed-off-by: Vishnu Alapati vishnu.alapati@tcs.com