Skip to content
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

How to log spring principal in request? #236

Closed
mrados7 opened this issue May 11, 2018 · 9 comments
Closed

How to log spring principal in request? #236

mrados7 opened this issue May 11, 2018 · 9 comments
Assignees

Comments

@mrados7
Copy link

mrados7 commented May 11, 2018

Is there any way to implement this?

@whiskeysierra
Copy link
Collaborator

This should work:

final class PrincipalHttpLogFormatter implements HttpLogFormatter {

    private final JsonHttpLogFormatter delegate;

    PrincipalHttpLogFormatter(final JsonHttpLogFormatter delegate) {
        this.delegate = delegate;
    }

    @Override
    public String format(final Precorrelation<HttpRequest> precorrelation) throws IOException {
        final Map<String, Object> content = delegate.prepare(precorrelation);
        content.put("principal", getPrincipal());
        return delegate.format(content);
    }

    @Override
    public String format(final Correlation<HttpRequest, HttpResponse> correlation) throws IOException {
        final Map<String, Object> content = delegate.prepare(correlation);
        content.put("principal", getPrincipal());
        return delegate.format(content);
    }

    private String getPrincipal() {
        final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        @Nullable final String principal = authentication.getName();
        return principal == null ? "anonymous" : principal;
    }

}

@whiskeysierra
Copy link
Collaborator

The only place to add this within logbook would be the spring-boot-starter, but I'm hesitant to do it, since it doesn't feel right to only support this for the json format.

@mrados7
Copy link
Author

mrados7 commented May 15, 2018

Thank you but i don't understand where can i configure my logbook?

Logbook logbook = Logbook.builder()
    .condition(new CustomCondition())
    .rawRequestFilter(new CustomRawRequestFilter())
    .rawResponseFilter(new CustomRawResponseFilter())
    .queryFilter(new CustomQueryFilter())
    .headerFilter(new CustomHeaderFilter())
    .bodyFilter(new CustomBodyFilter())
    .requestFilter(new CustomRequestFilter())
    .responseFilter(new CustomResponseFilter())
    .formatter(new CustomHttpLogFormatter())
    .writer(new CustomHttpLogWriter())
    .build();

In which class i can do that, to apply my PrincipalHttpLogFormatter?

@whiskeysierra
Copy link
Collaborator

.formatter(new CustomHttpLogFormatter())
.formatter(new PrincipalHttpLogFormatter(new JsonHttpLogFormatter())

@mrados7
Copy link
Author

mrados7 commented May 15, 2018

I understood that, but where should i put that configuration in which class?

@whiskeysierra
Copy link
Collaborator

If you're using the spring boot starter, then you can just override that part of the auto configuration:

@Bean
public HttpLogFormatter httpLogFormatter(final ObjectMapper mapper) {
    return new PrincipalHttpLogFormatter(new JsonHttpLogFormatter(mapper));
}

@mrados7
Copy link
Author

mrados7 commented May 15, 2018

Thank you very much :)

@mariusstaicu
Copy link

@whiskeysierra how about in a webflux application ?
it seems this does not work for a ReactiveSecurityContextHolder.

@thumatiramesh
Copy link

Hello folks,

I am banging my head from last today as I can't able to print the JSON log in the beautified format in the IntelliJ console rather it is printing the whole JSON in a single line. Can anyone please help me with this?

{"type":"REQUEST","correlation":"d8d965d73ba59297","method":"POST","uri":"http://localhost:8080/students","host":"localhost","path":"/students","scheme":"http","port":"8080","headers":{"content-length":["714"],"content-type":["application/json"],"host":["localhost:8080"],"postman-token":["ed949fad-61cb-4ff9-8353-8b97046c0ed6"]},"body":{"type":"REQUEST","correlation":"9f85d3d4c9ca43d9","protocol":"HTTP/1.1","remote":"0:0:0:0:0:0:0:1","method":"POST","uri":"http://localhost:8080/students","host":"localhost","path":"/students","scheme":"http","port":"8080","headers":{"content-length":["82"],"content-type":["application/json"],"host":["localhost:8080"],"postman-token":["9c4454db-9905-4c68-bf21-deb44ecb2f62"]},"body":{"studentName":"Ramesh","studentAge":50,"address":"Nellore"},"principal":"ramesh"}}

mail: rameshrathnat@gmail.com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants