I am setting up an OAuth2 provider in my groovy grails project. I can access the url but I always get a 401 Unauthorized error when I try to send a post request to /oauth/token The request goes as follows:
http://localhost:8080/test/oauth/token?username=user&password=password&scope=read&grant_type=password&client_id=client-id
I've followed the grails documentation for setting up the Oauth provider. But when I try to do the post request I get an error in my console No Session found for current thread now I have no idea where this comes from. error. Here is the hibernate error:
XNIO-1 task-3 2021-06-23 11:12:32,715 ERROR c.s.i.StandardInterceptor StandardInterceptor before: No Session found for current thread org.hibernate.HibernateException: No Session found for current thread
at org.grails.orm.hibernate.GrailsSessionContext.currentSession(GrailsSessionContext.java:112) ~[grails-datastore-gorm-hibernate5-7.0.4.RELEASE.jar:na]
at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:475) ~[hibernate-core-5.4.21.Final.jar:5.4.21.Final]
...
I've tried multiple different filterchains. And I've added the link to my url_public but it gave me a 404 not found So I settled on not putting in in the url_public and I got these filterchain:
grails.plugin.springsecurity.filterChain.chainMap = [
[pattern: '/oauth/token', filters: 'JOINED_FILTERS,-oauth2ProviderFilter,-securityContextPersistenceFilter,-logoutFilter,-authenticationProcessingFilter,-rememberMeAuthenticationFilter,-exceptionTranslationFilter'],
[pattern: '/securedOAuth2Resources/**', filters: 'JOINED_FILTERS,-securityContextPersistenceFilter,-logoutFilter,-authenticationProcessingFilter,-rememberMeAuthenticationFilter,-oauth2BasicAuthenticationFilter,-exceptionTranslationFilter'],
[pattern: '/**', filters: 'JOINED_FILTERS,-basicAuthenticationFilter,-basicExceptionTranslationFilter,-statelessSecurityContextPersistenceFilter,-oauth2ProviderFilter,-clientCredentialsTokenEndpointFilter,-oauth2BasicAuthenticationFilter,-oauth2ExceptionTranslationFilter']
]
These are my staticrules:
grails.plugin.springsecurity.controllerAnnotations.staticRules = [
[pattern: '/oauth/authorize', access: "(request.getMethod().equals('GET') or request.getMethod().equals('POST'))"],
[pattern: '/oauth/token', access: "request.getMethod().equals('POST')"],
]
Futhermore I checked in the database if the OAuthClient table was there and if I had an entry (From the bootstrap.groovy file that initiates an OAuthClient like this:)
new OAuthClient(
clientId: 'client-id',
authorizedGrantTypes: ['refresh_token', 'password', 'client_credentials'],
authorities: ['ROLE_CLIENT'],
scopes: ['read', 'write'],
).save(flush: true)