Spring Security Principal transformation
Is there a way to transform a Spring Security Principal before it is injected in a RestController method?
Let's say I have defined the following class:
@RestController
public class MyController {
@GetMapping("/test")
public void getWithPrincipalA(@AuthenticationPrincipal PrincipalTypeA a) {
...
}
@GetMapping("/test")
public void getWithPrincipalB(@AuthenticationPrincipal PrincipalTypeB b) {
...
}
}
I know that these controller methods are ambiguous and I could do several things to solve that, but what I would rather do is transform the @AuthenticationPrincipal
to some type I can define myself. The result would become something like:
@RestController
public class MyController {
@GetMapping("/test")
public void getWithTransformedPrincipal(@AuthenticationPrincipal MyTransformedPrincipal principal) {
...
}
}
Now I basically could define a single controller for several different authentication principals, without having to change the API.
Any help would be appreciated :)
java spring rest spring-security
|
show 4 more comments
Is there a way to transform a Spring Security Principal before it is injected in a RestController method?
Let's say I have defined the following class:
@RestController
public class MyController {
@GetMapping("/test")
public void getWithPrincipalA(@AuthenticationPrincipal PrincipalTypeA a) {
...
}
@GetMapping("/test")
public void getWithPrincipalB(@AuthenticationPrincipal PrincipalTypeB b) {
...
}
}
I know that these controller methods are ambiguous and I could do several things to solve that, but what I would rather do is transform the @AuthenticationPrincipal
to some type I can define myself. The result would become something like:
@RestController
public class MyController {
@GetMapping("/test")
public void getWithTransformedPrincipal(@AuthenticationPrincipal MyTransformedPrincipal principal) {
...
}
}
Now I basically could define a single controller for several different authentication principals, without having to change the API.
Any help would be appreciated :)
java spring rest spring-security
Why? Why not just create a single method and dispatch yourself?
– M. Deinum
Nov 20 '18 at 11:23
What kind of transformation are you talking about with what goal?
– Bart
Nov 20 '18 at 11:23
The@AuthenticationPrincipal
is injected by Spring Security, but when you have different authentication mechanisms (cookie, token, etc) thePrincipal
could be different. My rest API will be the same, though. If I could catch the differentPrincipal
s and convert them to a generic one that myRestController
could handle, I wouldn't have to duplicate my controller methods.
– Dormouse
Nov 20 '18 at 11:26
In my previous comment I misunderstood your comment. I would do what M. Deinum suggests. Transform the principal within the controller method and dispatch that generic principal to others.
– Bart
Nov 20 '18 at 11:27
1
You could ultimately use a argument resolver to get the authication principal from the security context and transform it from there.
– Bart
Nov 20 '18 at 11:39
|
show 4 more comments
Is there a way to transform a Spring Security Principal before it is injected in a RestController method?
Let's say I have defined the following class:
@RestController
public class MyController {
@GetMapping("/test")
public void getWithPrincipalA(@AuthenticationPrincipal PrincipalTypeA a) {
...
}
@GetMapping("/test")
public void getWithPrincipalB(@AuthenticationPrincipal PrincipalTypeB b) {
...
}
}
I know that these controller methods are ambiguous and I could do several things to solve that, but what I would rather do is transform the @AuthenticationPrincipal
to some type I can define myself. The result would become something like:
@RestController
public class MyController {
@GetMapping("/test")
public void getWithTransformedPrincipal(@AuthenticationPrincipal MyTransformedPrincipal principal) {
...
}
}
Now I basically could define a single controller for several different authentication principals, without having to change the API.
Any help would be appreciated :)
java spring rest spring-security
Is there a way to transform a Spring Security Principal before it is injected in a RestController method?
Let's say I have defined the following class:
@RestController
public class MyController {
@GetMapping("/test")
public void getWithPrincipalA(@AuthenticationPrincipal PrincipalTypeA a) {
...
}
@GetMapping("/test")
public void getWithPrincipalB(@AuthenticationPrincipal PrincipalTypeB b) {
...
}
}
I know that these controller methods are ambiguous and I could do several things to solve that, but what I would rather do is transform the @AuthenticationPrincipal
to some type I can define myself. The result would become something like:
@RestController
public class MyController {
@GetMapping("/test")
public void getWithTransformedPrincipal(@AuthenticationPrincipal MyTransformedPrincipal principal) {
...
}
}
Now I basically could define a single controller for several different authentication principals, without having to change the API.
Any help would be appreciated :)
java spring rest spring-security
java spring rest spring-security
edited Nov 20 '18 at 15:12
Dormouse
asked Nov 20 '18 at 11:15
DormouseDormouse
96811424
96811424
Why? Why not just create a single method and dispatch yourself?
– M. Deinum
Nov 20 '18 at 11:23
What kind of transformation are you talking about with what goal?
– Bart
Nov 20 '18 at 11:23
The@AuthenticationPrincipal
is injected by Spring Security, but when you have different authentication mechanisms (cookie, token, etc) thePrincipal
could be different. My rest API will be the same, though. If I could catch the differentPrincipal
s and convert them to a generic one that myRestController
could handle, I wouldn't have to duplicate my controller methods.
– Dormouse
Nov 20 '18 at 11:26
In my previous comment I misunderstood your comment. I would do what M. Deinum suggests. Transform the principal within the controller method and dispatch that generic principal to others.
– Bart
Nov 20 '18 at 11:27
1
You could ultimately use a argument resolver to get the authication principal from the security context and transform it from there.
– Bart
Nov 20 '18 at 11:39
|
show 4 more comments
Why? Why not just create a single method and dispatch yourself?
– M. Deinum
Nov 20 '18 at 11:23
What kind of transformation are you talking about with what goal?
– Bart
Nov 20 '18 at 11:23
The@AuthenticationPrincipal
is injected by Spring Security, but when you have different authentication mechanisms (cookie, token, etc) thePrincipal
could be different. My rest API will be the same, though. If I could catch the differentPrincipal
s and convert them to a generic one that myRestController
could handle, I wouldn't have to duplicate my controller methods.
– Dormouse
Nov 20 '18 at 11:26
In my previous comment I misunderstood your comment. I would do what M. Deinum suggests. Transform the principal within the controller method and dispatch that generic principal to others.
– Bart
Nov 20 '18 at 11:27
1
You could ultimately use a argument resolver to get the authication principal from the security context and transform it from there.
– Bart
Nov 20 '18 at 11:39
Why? Why not just create a single method and dispatch yourself?
– M. Deinum
Nov 20 '18 at 11:23
Why? Why not just create a single method and dispatch yourself?
– M. Deinum
Nov 20 '18 at 11:23
What kind of transformation are you talking about with what goal?
– Bart
Nov 20 '18 at 11:23
What kind of transformation are you talking about with what goal?
– Bart
Nov 20 '18 at 11:23
The
@AuthenticationPrincipal
is injected by Spring Security, but when you have different authentication mechanisms (cookie, token, etc) the Principal
could be different. My rest API will be the same, though. If I could catch the different Principal
s and convert them to a generic one that my RestController
could handle, I wouldn't have to duplicate my controller methods.– Dormouse
Nov 20 '18 at 11:26
The
@AuthenticationPrincipal
is injected by Spring Security, but when you have different authentication mechanisms (cookie, token, etc) the Principal
could be different. My rest API will be the same, though. If I could catch the different Principal
s and convert them to a generic one that my RestController
could handle, I wouldn't have to duplicate my controller methods.– Dormouse
Nov 20 '18 at 11:26
In my previous comment I misunderstood your comment. I would do what M. Deinum suggests. Transform the principal within the controller method and dispatch that generic principal to others.
– Bart
Nov 20 '18 at 11:27
In my previous comment I misunderstood your comment. I would do what M. Deinum suggests. Transform the principal within the controller method and dispatch that generic principal to others.
– Bart
Nov 20 '18 at 11:27
1
1
You could ultimately use a argument resolver to get the authication principal from the security context and transform it from there.
– Bart
Nov 20 '18 at 11:39
You could ultimately use a argument resolver to get the authication principal from the security context and transform it from there.
– Bart
Nov 20 '18 at 11:39
|
show 4 more comments
1 Answer
1
active
oldest
votes
Too keep things simple and transparant you could simply transform the principal in your controller method and dispatch the generic principal from there.
@RestController
public class MyController {
@GetMapping("/test")
public void getWithTransformedPrincipal(@AuthenticationPrincipal Principal principal) {
GenericPrincipal generic = PrincipalTransformer.transform(principal);
doSomethingWithPrincipal(generic);
}
}
Any chance of adding the argument resolver solution as an alternative?
– Dormouse
Nov 20 '18 at 15:20
I would rather not. Not because I'm unwilling. More because I think that it would distract from the more obvious and simple solution.
– Bart
Nov 21 '18 at 8:09
Right. Although that was the original question :)
– Dormouse
Nov 21 '18 at 8:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391811%2fspring-security-principal-transformation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Too keep things simple and transparant you could simply transform the principal in your controller method and dispatch the generic principal from there.
@RestController
public class MyController {
@GetMapping("/test")
public void getWithTransformedPrincipal(@AuthenticationPrincipal Principal principal) {
GenericPrincipal generic = PrincipalTransformer.transform(principal);
doSomethingWithPrincipal(generic);
}
}
Any chance of adding the argument resolver solution as an alternative?
– Dormouse
Nov 20 '18 at 15:20
I would rather not. Not because I'm unwilling. More because I think that it would distract from the more obvious and simple solution.
– Bart
Nov 21 '18 at 8:09
Right. Although that was the original question :)
– Dormouse
Nov 21 '18 at 8:51
add a comment |
Too keep things simple and transparant you could simply transform the principal in your controller method and dispatch the generic principal from there.
@RestController
public class MyController {
@GetMapping("/test")
public void getWithTransformedPrincipal(@AuthenticationPrincipal Principal principal) {
GenericPrincipal generic = PrincipalTransformer.transform(principal);
doSomethingWithPrincipal(generic);
}
}
Any chance of adding the argument resolver solution as an alternative?
– Dormouse
Nov 20 '18 at 15:20
I would rather not. Not because I'm unwilling. More because I think that it would distract from the more obvious and simple solution.
– Bart
Nov 21 '18 at 8:09
Right. Although that was the original question :)
– Dormouse
Nov 21 '18 at 8:51
add a comment |
Too keep things simple and transparant you could simply transform the principal in your controller method and dispatch the generic principal from there.
@RestController
public class MyController {
@GetMapping("/test")
public void getWithTransformedPrincipal(@AuthenticationPrincipal Principal principal) {
GenericPrincipal generic = PrincipalTransformer.transform(principal);
doSomethingWithPrincipal(generic);
}
}
Too keep things simple and transparant you could simply transform the principal in your controller method and dispatch the generic principal from there.
@RestController
public class MyController {
@GetMapping("/test")
public void getWithTransformedPrincipal(@AuthenticationPrincipal Principal principal) {
GenericPrincipal generic = PrincipalTransformer.transform(principal);
doSomethingWithPrincipal(generic);
}
}
answered Nov 20 '18 at 11:35
BartBart
14.3k34372
14.3k34372
Any chance of adding the argument resolver solution as an alternative?
– Dormouse
Nov 20 '18 at 15:20
I would rather not. Not because I'm unwilling. More because I think that it would distract from the more obvious and simple solution.
– Bart
Nov 21 '18 at 8:09
Right. Although that was the original question :)
– Dormouse
Nov 21 '18 at 8:51
add a comment |
Any chance of adding the argument resolver solution as an alternative?
– Dormouse
Nov 20 '18 at 15:20
I would rather not. Not because I'm unwilling. More because I think that it would distract from the more obvious and simple solution.
– Bart
Nov 21 '18 at 8:09
Right. Although that was the original question :)
– Dormouse
Nov 21 '18 at 8:51
Any chance of adding the argument resolver solution as an alternative?
– Dormouse
Nov 20 '18 at 15:20
Any chance of adding the argument resolver solution as an alternative?
– Dormouse
Nov 20 '18 at 15:20
I would rather not. Not because I'm unwilling. More because I think that it would distract from the more obvious and simple solution.
– Bart
Nov 21 '18 at 8:09
I would rather not. Not because I'm unwilling. More because I think that it would distract from the more obvious and simple solution.
– Bart
Nov 21 '18 at 8:09
Right. Although that was the original question :)
– Dormouse
Nov 21 '18 at 8:51
Right. Although that was the original question :)
– Dormouse
Nov 21 '18 at 8:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391811%2fspring-security-principal-transformation%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why? Why not just create a single method and dispatch yourself?
– M. Deinum
Nov 20 '18 at 11:23
What kind of transformation are you talking about with what goal?
– Bart
Nov 20 '18 at 11:23
The
@AuthenticationPrincipal
is injected by Spring Security, but when you have different authentication mechanisms (cookie, token, etc) thePrincipal
could be different. My rest API will be the same, though. If I could catch the differentPrincipal
s and convert them to a generic one that myRestController
could handle, I wouldn't have to duplicate my controller methods.– Dormouse
Nov 20 '18 at 11:26
In my previous comment I misunderstood your comment. I would do what M. Deinum suggests. Transform the principal within the controller method and dispatch that generic principal to others.
– Bart
Nov 20 '18 at 11:27
1
You could ultimately use a argument resolver to get the authication principal from the security context and transform it from there.
– Bart
Nov 20 '18 at 11:39