jhipster / generator-jhipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.
https://www.jhipster.tech
Apache License 2.0
21.58k stars 4.02k forks source link

Code generation error with Reactive, JWT & Websockets selected #14567

Open aricooperman opened 3 years ago

aricooperman commented 3 years ago
Overview of the issue

When you select reactive, websockets and JWT the code generated won't compile.In JWTFilter::resolveToken, the new reactive type for ServerHttpRequest does not have a getParameter method. This code does not exist if you don't select websockets.

JWTFilter.java:43: error: cannot find symbol String jwt = request.getParameter(AUTHORIZATION_TOKEN); ^ symbol: method getParameter(String) location: variable request of type ServerHttpRequest

Additionally, there are some warnings related to JWT generated code (not a direct issue with this bug but just FYI):

In JWTFilter, Mono::subscriberContext is deprecated

return chain.filter(exchange).subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication));

And in TokenProvider, there is a n unchecked warning for the JacksonSerializer is not parameterized

return Jwts .builder() .... .serializeToJsonWith(new JacksonSerializer()) ....

Motivation for or Use Case

Code won't build once generated

Reproduce the error

Should be reproducible doing a monolith (but should also affect microservices) with reactive to true and security set to JWT

Related issues

None found

Suggest a Fix

In JWTFilter replace

String jwt = request.getParameter(AUTHORIZATION_TOKEN);

with

String jwt = request.getQueryParams().getFirst(AUTHORIZATION_TOKEN);

JHipster Version(s)

7.0.1

JHipster configuration

This JDL will reproduce

JDL definitions
application {
  config {
    applicationType monolith
    authenticationType jwt
    baseName reactivejwtbug
    buildTool gradle
    packageName com.foo.bar
    reactive true
    websocket spring-websocket
  }

  entities *
}

entity A {
  a Boolean required
}
  
Browsers and Operating System

Fedora 33 Not browser related

aricooperman commented 3 years ago

Additionally I have found, once you make that fix so it compiles and start it via boot run, seems there is another misconfiguration for websockets with this exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stompWebSocketHandlerMapping' defined in class path resource [org/springframework/web/socket/config/annotation/DelegatingWebSocketMessageBrokerConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'stompWebSocketHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No suitable default RequestUpgradeStrategy found at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:63) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) at com.foo.bar.ReactivejwtbugApp.main(ReactivejwtbugApp.java:69) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'stompWebSocketHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No suitable default RequestUpgradeStrategy found at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ... 23 common frames omitted Caused by: java.lang.IllegalStateException: No suitable default RequestUpgradeStrategy found at org.springframework.web.socket.server.support.AbstractHandshakeHandler.initRequestUpgradeStrategy(AbstractHandshakeHandler.java:159) at org.springframework.web.socket.server.support.AbstractHandshakeHandler.<init>(AbstractHandshakeHandler.java:122) at org.springframework.web.socket.server.support.DefaultHandshakeHandler.<init>(DefaultHandshakeHandler.java:35) at com.foo.bar.config.WebsocketConfiguration$2.<init>(WebsocketConfiguration.java:77) at com.foo.bar.config.WebsocketConfiguration.defaultHandshakeHandler(WebsocketConfiguration.java:77) at com.foo.bar.config.WebsocketConfiguration.registerStompEndpoints(WebsocketConfiguration.java:43) at org.springframework.web.socket.config.annotation.DelegatingWebSocketMessageBrokerConfiguration.registerStompEndpoints(DelegatingWebSocketMessageBrokerConfiguration.java:60) at org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurationSupport.stompWebSocketHandlerMapping(WebSocketMessageBrokerConfigurationSupport.java:91) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ... 24 common frames omitted

med92 commented 3 years ago

I have the same problem here. i tried to disable reactive in my JDL but it always turn to be true in my yo-rc file

mraible commented 3 years ago

i tried to disable reactive in my JDL but it always turn to be true in my yo-rc file

If you're creating a gateway, reactive is forced. This is because Spring Cloud no longer supports a non-reactive gateway with Zuul in its latest version.

mraible commented 3 years ago

Confirmed with the JDL provided.

> Task :compileJava
Note: Hibernate JPA 2 Static-Metamodel Generator 5.4.29.Final
/Users/mraible/Downloads/567/src/main/java/com/foo/bar/security/jwt/JWTFilter.java:43: error: cannot find symbol
        String jwt = request.getParameter(AUTHORIZATION_TOKEN);
                            ^
  symbol:   method getParameter(String)
  location: variable request of type ServerHttpRequest
Note: /Users/mraible/Downloads/567/src/main/java/com/foo/bar/security/jwt/JWTFilter.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/mraible/Downloads/567/src/main/java/com/foo/bar/security/jwt/TokenProvider.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error

> Task :compileJava FAILED

I'll add a bounty in hopes that someone creates a PR to fix.

sendilkumarn commented 3 years ago

It looks like we need to completely re-do the websocket configuration here for the reactive application.

P.S. Can we increase the bounty here?

mraible commented 3 years ago

@sendilkumarn Done! Raised to 500.

sendilkumarn commented 3 years ago

We don't support websocket for a reactive application, when you generate the application via the command line.

@MathieuAA do you know why we allow this via JDL?

MathieuAA commented 3 years ago

No idea. I don't expect the fix to take long to implement.

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 30 days with no activity. Our core developers tend to be more verbose on denying. If there is no negative comment, possibly this feature will be accepted. We are accepting PRs :smiley:. Comment or this will be closed in 7 days

atomfrede commented 3 years ago

keep it open,

MathieuAA commented 3 years ago

We don't support websocket for a reactive application, when you generate the application via the command line.

If we don't, then the application validator should be updated. See this PR for the JDL-related fix.

MathieuAA commented 3 years ago

Reopening, not sure this issue only needs a change in the JDL-related code to be truly closed.

DanielFran commented 3 years ago

In the past websocket was implemented only for non-reactive gateways, but since it was mandatory to switch gateways to reactive now it is missing this functionality. So, yes I agree this needs new development.

vwiencek commented 3 years ago

Hello I looked at this article https://github.com/MaxNeutrino/snippets/tree/master/webflux-websocket to try to add websocket support to JHipster gateways generated by version 7.1.0 but I ended up with a strange behavior.

I created a very simple gateway project here https://github.com/vwiencek/jhipster-websocket

Expected behavior

Current behavior

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 30 days with no activity. Our core developers tend to be more verbose on denying. If there is no negative comment, possibly this feature will be accepted. We are accepting PRs :smiley:. Comment or this will be closed in 7 days