Skip to content

Conversation

@vishnu-alapati
Copy link
Contributor

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

public void commence(HttpServletRequest request, HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
if (authException instanceof BadCredentialsException) {
LOGGER.info("Bad Credentials {}", HttpStatus.UNAUTHORIZED);
Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

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

Copy link
Contributor Author

@vishnu-alapati vishnu-alapati Oct 9, 2023

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);
Copy link
Contributor

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:

  1. I think "User name: {}" is enough. Non-authenticated user cannot get here.
  2. The same log should be done for generatreAndPublish(). Please, don't copy the code, create a function instead.

Copy link
Contributor Author

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() {
Copy link
Contributor

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);
Copy link
Contributor

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: {}"

Copy link
Contributor

@z-sztrom z-sztrom left a 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.

@z-sztrom z-sztrom merged commit 1b741e0 into eiffel-community:master Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants