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

Propagate Authentication to SecurityContextHolder #234

Closed
jvmlet opened this issue Aug 24, 2021 · 9 comments
Closed

Propagate Authentication to SecurityContextHolder #234

jvmlet opened this issue Aug 24, 2021 · 9 comments
Labels
enhancement Auto-generates notes
Milestone

Comments

@jvmlet
Copy link
Collaborator

jvmlet commented Aug 24, 2021

See this discussion
@markbanierink, FYI

@jvmlet jvmlet added the enhancement Auto-generates notes label Aug 24, 2021
@jvmlet jvmlet added this to the 4.5.6 milestone Aug 24, 2021
@jvmlet
Copy link
Collaborator Author

jvmlet commented Aug 24, 2021

@markbanierink, would you please post a snippet here how you use @PreAuthorize annotation from grpc service

@markbanierink
Copy link

markbanierink commented Aug 24, 2021

Sure!

Our gRPC service looks pretty much like this:

@GRpcService
public class EntityServiceImpl extends EntityServiceGrpc.EntityServiceImplBase {
	
	@Autowired
	private DomainService domainService;

	@Override
	public void doFromRemote(EntityProtos.EntityRequest request, StreamObserver<EntityProtos.EntityResponse> responseObserver) {
		var entityA = request.getEntityA();

		var valB = domainService.doSomething(entityA);

		EntityProtos.Entity respVal = EntityProtos.Entity.newBuilder().setValue(valB).build();
		var responseBuilder = EntityProtos.EntityResponse.newBuilder();
        	responseBuilder.addSomeValue(respVal);

        	responseObserver.onNext(responseBuilder.build());
        	responseObserver.onCompleted();
	}

}

And the domain service:

@Component
public class DomainService {

	@PreAuthorize("hasPermission(#entityA.id, somePermission)")
	public Object doSomething(EntityA entityA) {
		// do something after permission was checked
	}

}

@jvmlet
Copy link
Collaborator Author

jvmlet commented Aug 24, 2021

Thanks @markbanierink. I've integrated your PR, had to refactor a bit because of merge conflicts. All tests are passing, I'll commit tomorrow. I'll also try to support @PreAuthorizeon grpc service /method. Currently it's working only for downstream services. I'll need to have special grpc method invoker to go through proxied grpc service bean in order to evaluate the annotation' s expression

@jvmlet
Copy link
Collaborator Author

jvmlet commented Aug 25, 2021

@markbanierink , did you have to add @EnableGlobalMethodSecurity(prePostEnabled = true) somewhere in your app to enable the enforcement of @PreAuthorize ?

@markbanierink
Copy link

Yes, it has to be configured:

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration {
    // some beans
}

@jvmlet
Copy link
Collaborator Author

jvmlet commented Aug 25, 2021

@markbanierink , as I understand, you don't use grpc security feature, right ? Only pure spring boot security infrastructure, right ?

@markbanierink
Copy link

markbanierink commented Aug 25, 2021

Euhm.. I'm not sure what you mean, but we do have a configuration for the grpc security too:

@Configuration
public class CustomGrpcSecurityConfigurerAdapter extends GrpcSecurityConfigurerAdapter {

    @Override
    public void configure(GrpcSecurity builder) throws Exception {
        builder.authorizeRequests().anyMethod().authenticated();
    }

    // some more configuration
}

But yes, for the rest we only use spring security.
In the suggested solution I proposed a way to propagate the Authentication from the grpc security context to the spring security context. So we use a combination of both.

@jvmlet jvmlet closed this as completed in 1c17622 Aug 25, 2021
@jvmlet
Copy link
Collaborator Author

jvmlet commented Aug 25, 2021

@markbanierink , I've committed the changes from your PR #233.
I'm talking about another issue I'm experiencing with @PreAuthorize support, lets continue at #175

@jvmlet
Copy link
Collaborator Author

jvmlet commented Aug 26, 2021

@markbanierink , 4.5.6 is out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Auto-generates notes
Projects
None yet
Development

No branches or pull requests

2 participants