Coder Social home page Coder Social logo

Comments (4)

teuber789 avatar teuber789 commented on June 14, 2024

Also - could this be related to #3 ?

from spring-addons.

ch4mpy avatar ch4mpy commented on June 14, 2024

Is it possible that you have different GrantedAuthoritiesMapper during tests and at runtime ? Can you double check and provide me with implementations used in both cases ?

As can be seen here, I use registered authoritiesMapper to transform provided authorities.
And as can be seen there, NullAuthoritiesMapper is used by default (no-op mapper).

Maybe do you have a SimpleAuthorityMapper at runtime (which adds ROLE_ prefix by default) and NullAuthoritiesMapper (which does nothing) wired during tests ?

What I would do in your case:

  1. search for a conf wiring a SimpleAuthorityMapper at runtime
  2. understand why this conf is ignored during tests (maybe conf file is not scanned, maybe GrantedAuthoritiesMapper is explicitly set to NullAuthoritiesMapper in test conf, ...)
  3. fix test conf

from spring-addons.

ch4mpy avatar ch4mpy commented on June 14, 2024

I got it:
You override GrantedAuthoritiesMapper with SimpleAuthoritiesMapper in KeycloakWebSecurityConfigurerAdapter but don't expose the bean. As a consequence my lib uses default one which is NullAuthoritiesMapper.

In KeycloakWebSecurityConfigurerAdapter, just replace

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) {
        KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
        keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
        auth.authenticationProvider(keycloakAuthenticationProvider);
    }

with

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) {
        KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
        keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(authoritiesMapper());
        auth.authenticationProvider(keycloakAuthenticationProvider);
    }
    
	@Bean GrantedAuthoritiesMapper authoritiesMapper() {
		return new SimpleAuthorityMapper();
	}

and your second test will pass.

P.S.
please close the ticket if this answers your question.

P.S. 2
maybe your web security conf can simplified a bit. I provide with a working sample in tests: https://github.com/ch4mpy/spring-addons/blob/master/spring-security-oauth2-test-webmvc-addons/src/test/java/com/c4_soft/springaddons/samples/webmvc/keycloak/KeycloakSpringBootSampleApp.java

from spring-addons.

teuber789 avatar teuber789 commented on June 14, 2024

Hey, that worked perfectly! I wasn't aware that I could needed to expose the authorities mapper bean in order to keep the role mapping between the service and the tests consistent. It's good to know now.

Many thanks!

from spring-addons.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.