The background display of the startup project is as follows - jwt

Spring integrates jwt to realize login.
pom.xml configuration
<!--security-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--JWT-->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
application.yml configuration
logging:
level:
com.xxxx.server.mapper: debug
jwt:
tokenHeader: Authorization
secret: yeb-secret
expiration: 604800
tokenHead: Bearer
Running the project background prompts the following information.
Description:
Failed to bind properties under 'logging.level.jwt.tokenheader' to org.springframework.boot.logging.LogLevel:
Property: logging.level.jwt.tokenheader
Value: "Authorization"
Origin: class path resource [config/application.yml] - 52:20
Reason: failed to convert java.lang.String to org.springframework.boot.logging.LogLevel (caused by java.lang.IllegalArgumentException: No enum constant org.springframework.boot.logging.LogLevel.Authorization)
Why it can't works?
thanks!!

Related

Failed connection when trying for Heroku PostgreSQL with Spring

I am trying to set up a Spring Boot application. Currently I am working in a local environment and I have a Heroku PostgreSQL db. When I start my application it starts with error:
HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: El intento de conexión falló.
...
Caused by: java.net.UnknownHostException: #host
...
HHH000342: Could not obtain connection to query metadata
Where I have added the correct hostname and hideen it for security. I know this hostname is right since I've managed to connect using an Express app.
My POM.xml has the following dependencies:}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
And my application.properties file is:
spring.datasource.url=jdbc:postgresql://#host:5432/dal2fiqv9or5eg?user=----&password=----&sslmode=require
spring.datasource.username=----
spring.datasource.password=----
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa-hibernate.ddl-auto=create-drop
spring.jpa.show=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
Whre once again I hid the host, username and pwd.
After this the application starts.
The spring.datasource.url property in your application.properties seems to be incorrect.
This file has worked for me:
#postgres parameters
spring.datasource.url=jdbc:postgresql://<host>:<port>/<database>
spring.datasource.username=<user>
spring.datasource.password=<password>
spring.jpa.show-sql=true
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
logging.level.org.springframework=TRACE
logging.level.org.hibernate.type=TRACE
server.port=${PORT:8080}
The values for port, host, database, user, and password can be derived from the HerokuPostgres addon you add to your Heroku app
Do not add # or any special symobols. Simply replace the values with the values you see in Heroku.
eg: spring.datasource.url=jdbc:postgresql://myhostname:5432/mydbname

camel 3.8 generates REST routes twice

I faced a problem with camel REST routes when I switched from camel version 3.5.0 to version 3.8.0.
In the version 3.8.0 all routes from the REST service are generated twice! So at start of the camel context the exception is thrown:
Failed to start route route7 because of Multiple consumers for the same endpoint is not allowed: jetty:http://0.0.0.0:6060/start?httpMethodRestrict=GET
In detail:
I have a simple RouteBuilder to generate REST routes:
#Component
public class RestInterface extends RouteBuilder {
#Override
public void configure() throws Exception {
restConfiguration()
.component("jetty")
.host(urlInfo.getHost())
.port(urlInfo.getPort())
.bindingMode(RestBindingMode.json)
.dataFormatProperty("prettyPrint", "true")
.clientRequestValidation(true);
rest("/")
.get("/start").outType(RepositoryPackageInfo.class).to("direct:start")
.get("/stop").outType(RepositoryPackageInfo.class).to("direct:stop");
// ...
}
}
When I start this code with camel 3.8.0 I get following console output:
...
jetty-9.4.31.v20200723; built: 2020-07-23T17:57:36.812Z; git: 450ba27947e13e66baa8cd1ce7e85a4461cacc1d; jvm 11.0.4+11
Started o.e.j.s.ServletContextHandler#5f65e0c0{/,null,AVAILABLE}
Started ServerConnector#27b7204{HTTP/1.1, (http/1.1)}{0.0.0.0:6060}
Started #4095ms
Error starting CamelContext (camel-1) due to exception thrown: Failed to start route route7 because of Multiple consumers for the same endpoint is not allowed: jetty:http://0.0.0.0:6060/start?httpMethodRestrict=GET
org.apache.camel.FailedToStartRouteException: Failed to start route route7 because of Multiple consumers for the same endpoint is not allowed: jetty:http://0.0.0.0:6060/start?httpMethodRestrict=GET
...
Apache Camel 3.8.0 (camel-1) shutting down (timeout:45s)
Stopped ServerConnector#27b7204{HTTP/1.1, (http/1.1)}{0.0.0.0:6060}
Stopped o.e.j.s.ServletContextHandler#5f65e0c0{/,null,UNAVAILABLE}
Routes shutdown summary (total:40 stopped:40) <-------------- total 40 routes!!!
...
Stopped route8 (rest://get:/:/stop)
Stopped route7 (rest://get:/:/start)
Stopped route2 (rest://get:/:/stop)
Stopped route1 (rest://get:/:/start) <-------------- the routes "stop" and "start" are doubled!!!
...
If I run the same code with camel 3.5.0, the application starts without exceptions, and camel has only 38 routes:
etty-9.4.31.v20200723; built: 2020-07-23T17:57:36.812Z; git: 450ba27947e13e66baa8cd1ce7e85a4461cacc1d; jvm 11.0.4+11
Started o.e.j.s.ServletContextHandler#6cbb6c41{/,null,AVAILABLE}
Started ServerConnector#42172065{HTTP/1.1, (http/1.1)}{0.0.0.0:6060}
Started #4034ms
Route: route1 started and consuming from: jetty:http://0.0.0.0:6060/start
Route: route2 started and consuming from: jetty:http://0.0.0.0:6060/stop
...
Total 38 routes, of which 38 are started <-------------- total 38routes!!!
...
Completed initialization in 16 ms
Thank you in advance. I hope somebody can help me with this issue.
For completeness, here is a part of my pom.xml
<!-- Camel -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-rest</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jetty</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-zipfile</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-zipfile-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-bean-starter</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-management</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-spring</artifactId>
<version>${camel.version}</version>
<scope>test</scope>
</dependency>
Check if configure method on your RestInterface RouteBuilder is getting called more than once. Multiple calls cause duplicate RouteDefinitions to be added to the RouteBuilder instance which can lead to this error.
One way to solve this is to use boolean value in your RouteBuilder to check if configure method has already been called. Then use this value to early out or to throw an exception.
boolean isConfigured = false;
#Override
public void configure() throws Exception {
if(isConfigured){
return;
}
// Configure routes
isConfigured = true;
}
Encountered similar problem with Camel 2.x running in Karaf + OSGi where modifying config file lead to same error.
Basically configuring CamelContext service caused it to restart while the RouteBuilder services it referenced kept their state leading to situation where their configure method got called the second time leading to same error.

Spring Boot admin not showing any client applications in dashboard

I have been following this tutorial for Spring boot Admin, but when I run the admin application I can't see any spring boot client application for monitoring. I have checked that both the admin and client apps are running. Admin is running on port 8080 and client application is running on port 9010
This is what I have done in my Admin application
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.3</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.3</version>
</dependency>
Added these annotations
#Configuration
#SpringBootApplication
#EnableAdminServer
public class SpringBootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdminApplication.class, args);
}
}
This is what I have added in my client application
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.3</version>
</dependency>
and added this in my properties file (YAML)
spring:
boot:
admin:
url:
- http://localhost:8080
management:
security:
enabled: false
This is what my Dashboard looks like, as you can see there are no applications to monitor.

Connecting Mongodb Atlas from Spring boot

I i am new to the Mongodb with Spring Boot.And i have MONGODB ATLAS URI Connection String
mongodb://userName:<PASSWORD>#icarat-shard-00-00-7lopx.mongodb.net:27017,icarat-shard-00-01-7lopx.mongodb.net:27017,icarat-shard-00-02-7lopx.mongodb.net:27017/<DATABASE>?ssl=true&replicaSet=icarat-shard-0&authSource=admin
Then in my Spring Boot Application i set uri in application.properties like
spring.data.mongodb.uri: mongodb://userName:*****#icarat-shard-00-00-7lopx.mongodb.net:27017,icarat-shard-00-01-7lopx.mongodb.net:27017,icarat-shard-00-02-7lopx.mongodb.net:27017/vehicleplatform?ssl=true&replicaSet=icarat-shard-0&authSource=admin
This Repository intefface
public interface OrganizationRepository extends MongoRepository<Organization, String> {
}
when i Inject OrganizationRepository interface Its showing error like this
Failed to instantiate [com.mongodb.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.IllegalArgumentException: The connection string contains invalid user information. If the username or password contains a colon (:) or an at-sign (#) then it must be urlencoded
And this my Document class
#Document(collection="Organization")
public class Organization {
#Id
String id;
String orgName;
String orgAddress;
String pinCode;
//getter
//setter
}
This is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.icarat</groupId>
<artifactId>vehicleplatform</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>vehicleplatform</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mongodb java driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.0</version>
</dependency>
<!-- swagger2 dependency -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId> springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
How we can fix this? Thanks
I find the actual problem. In my pevious Atlas password contain # (symbol) so its not supoort. I follwed document HERE
IMPORTANT If the password contains reserved URI characters, you must
escape the characters per RFC 2396. For example, if your password is
#bc123, you must escape the # character when specifying the password
in the connection string; i.e. %40bc123.
Failed to instantiate [com.mongodb.MongoClient]: Factory method
'mongo' threw exception; nested exception is
java.lang.IllegalArgumentException: The connection string contains
invalid user information. If the username or password contains a colon
(:) or an at-sign (#) then it must be urlencoded
The problem is crystal clear, if you are trying to connect with MongoDB Atlas with SpringBoot, your password must be URL Encoded which is more secure.
Here you can see how to URL encode password in MongoDB Atlas.
You can use the Mongodb SRV record to define all nodes associated to the cluster
# Mongo Connection
spring.data.mongodb.uri=mongodb+srv://myuser:mypassword#mycluster.azure.mongodb.net/mydb?retryWrites=true&w=majority
where
myuser: Mongodb user
mypassword: password of the user above
mycluster.azure.mongodb.net: host of your cluster
mydb: Mongodb database of your application
It is important to use the correct SRV assigned to you by Atlas (in your example you have combined all three shards), which you can get in the "Connect Your Application" screen, together with a code example (if needed).
Copy the connection string, replace password and dbname, set this in the SpringBoot property file.
I think best escape is to follow this pattern:
spring.data.mongodb.authentication-database= # Authentication database name.
spring.data.mongodb.database=db_name.
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.password=passwrd
spring.data.mongodb.port=27017
spring.data.mongodb.repositories.enabled=true # Enable Mongo repositories.
spring.data.mongodb.username= # Login user of the mongo server.
In my case I'm added {} surrounded with the documentDb url
I faced same issue when connecting to Mongo DB Atlas from spring boot:
'If the username or password contains a colon (:) or an at-sign (#) then it must be urlencoded'
This occurs if the password contains above mentioned special characters,
I was able to solve the issue by using the encoded password.
Go to https://www.urlencoder.io/ -> type the password to be encoded, Example: 'sample#password' , encoded string will be 'sample%40password'. Now use this in your connection uri by replacing the password
the password value will look something like
mongodb://userName:sample%40password#icarat-shard-00-00-7lopx.mongodb.net:27017,icarat-shard-00-01-7lopx.mongodb.net:27017,icarat-shard-00-02-7lopx.mongodb.net:27017/testdb?ssl=true&replicaSet=icarat-shard-0&authSource=admin

JAX-RS Jersey Error java.lang.NoSuchMethodError

I wanted to implement a file-upload function for my jersey based rest server.
when i set my pom.xml (using maven) to:
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.0</version>
</dependency>
i get following error:
JAX-RS Jersey Error java.lang.NoSuchMethodError: org.glassfish.jersey.internal.util.ReflectionHelper.getContextClassLoaderPA()Ljava/security/PrivilegedAction;
without the "jersey-media-multipart"-dependency the rest server is working but i cant use the file-upload functions.
Following the important part of source code:
ResourceConfig resourceConfig = new ResourceConfig(RestWebServer.class);
//resourceConfig.register(MultiPartFeature.class);
URI endPoint = new URI(pathServer);
server = JdkHttpServerFactory.createHttpServer( endPoint, resourceConfig );
RestWebserver.java:
#Path("/fileupload")
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
#FormParam("file") InputStream uploadedInputStream,
#FormParam("file") FormDataContentDisposition fileDetail)
{
String uploadedFileLocation = "c://" + fileDetail.getFileName();
// save it
saveToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded via Jersey based RESTFul Webservice to: " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
Not a Jersey user so I am just guessing, but you probably have a jar mismatch.
Try replacing your second entry with this:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.4.1</version>
</dependency>
Basing my guess on the chapter 8, have you registered your client/server code?