Springboot server-side chart creation - java

I want to create Google Charts at server-end. I found some plugins like -
GWT Tutorial, but I did not find it helpful because we need client-side interaction here. I need something using which I can create charts at server end so that I can store these charts images without requiring user to interact. I also found one more plugin - Charts4j, but I did not find any helpful examples or tutorial for this plugin.
Update-
Using Thymeleaf, I was able to load the HTML file to backend and was able to convert the HTML as PDF using HTMLToPDF plugin. The HTML is working beautifully fine. The PDF is getting generated. Now only issue left is that the javascript from HTML is not getting evaluated. Any solution for this?
Another Update -
When I find a solution I find another obstacle. I was able to execute the script using ScripEngine, but ScriptEngine did not find document. Then I used Java Jsoup's Document, sent the object to ScriptEngine, but again Jsoup's Document has different methods than Javascript's document.

I found a solution. I solved my issue using Thymeleaf, Selenium and IText HTML2PDF. I followed given steps -
I added below dependencies to my POM -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.0.3</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>4.0.4</version>
</dependency>
Then, I resolved my HTML template containing javascript using ThymeleafResolver -
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setPrefix("/<My folder name in resources>/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML");
return templateResolver;
}
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setTemplateEngineMessageSource(messageSource());
return templateEngine;
}
And in my code I used -
Context ctx = new Context();
String html = templateEngine.process(<My HTML file name>, ctx);
This resolved my HTML. Now, about javascript, I referred this - Evaludate JS, and my issue was resolved.
Thank you.

Related

Httpclient and Zipkin Java

My and project team are looking to add Zipkin logging and tracing to our current project. We are working in an microservice environment using Spring Boot (Java 17) and cloud foundery. For the communication between Microservices we are using HttpClient. From what I've gathered from the documentation Zipkin requires an RestTemplate to function. However we don't have time to change this.
We were able to implement Zipkin in every individual project. However, every call generates their own Trace ID. I think we need to configure the HttpClient to work in tandem with Zipkin, however the documentation is not very clear and I have been unable to find anything that explains how to do this.
What can I try on this? I've included the config and dependencies below.
spring:
application:
name: Application_1
zipkin:
baseUrl: http://localhost:9411
sleuth:
sampler:
probability: 1.0
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>3.1.3</version>
</dependency>

No Multipart Configuration for Spring boot version 2.3.1

I am creating an application and want to send user data along with his profile picture. I am using reactjs for frontend and java 1.8 with spring boot 2.3.1. I researched for 3 days and everywhere I see that we can send the user data and profile picture file as multipart form data from UI and then do the following in my controller to get the two different data.
public ResponseEntity<String> saveUser(#RequestParam("user") String stringUserRequest, #RequestParam("file") MultipartFile file)
Here the user data is actually a JSON but is converted to a string and sent from UI.
But this keeps throwing the below error
org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided
I have searched a lot and it has something to do with the servelet version being higher than 3.0. There are a lot of questions regarding the same but all of them are for spring framework and not for spring boot in particular and have tried them also but nothing seems to work.
Can anyone tell me either how to create this Multipart Config for spring boot or how to degrade the servelet version?
You need to configure a multipart resolver in your configuration class, first you add these dependencies in your POM:
<!-- multipart resolver-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
and you need to declare this Bean configuration:
#Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
return multipartResolver;
}
Finally, don't forget to specify the content type as multipart/form-data when sending your request.
EDIT:
the header Content-type should be multipart/form-data;boundary=--------------------------362947062764690924037801

sec:authorize does not work - Spring Boot 2, Thymeleaf 3, Thymeleaf Spring Security 5 integration package

I'm working with
Spring Boot 2.2.5
Thymeleaf 3.0.11
Thymeleaf Spring Security 5, 3.0.4 Release
I use the following dependencies in my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
Versions are recognized and the respective jars are included in my project. I also have added the extra namespace of Thymeleaf Security Module to my templates:
<html xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
I have implemented a role based authentication & authorization with Hibernate and MySQL and login / logout, which for my understanding works fine.
The sec:authentication tag of Thymeleaf Security Dialect works fine and also displays the correct roles. The sec:authorize tag seems to work as well when calling the method sec:authorize="isAuthenticated()".
However, I'm struggling when evaluating the role of a user in the templates, both using sec:authorize="hasRole('...')" or th:if="${#authorization.expression('hasRole(''...'')')}". I seems that the roles cannot be evaluated although they are correctly displayed using sec:authentication="principal.authorities".
I have summarized my approaches on a test page, displaying the following result:
Any clue where my bug or misunderstanding hides? Many thanks for your support in advance.
After some more research I figured out my issue. After adding the prefix ROLE_ to the role names (in the datasource), everything works fine.

Use BCrypt hashing function in Spring Boot without all the overkill security?

I have a Spring Boot project with SQL/Web dependencies. I have controllers and models but NO configuration classes. This is a very simple project so I'm doing simple authentication by checking user-specific tokens in the request headers. I would like to use the BCrypt dependency to hash passwords before saving them into my database, but Spring Boot won't let me simply use the static functions.
I have added these three dependencies to my pom.xml:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
And created an endpoint in my controller just to check the output of the static hashpw function.
#GetMapping("/bcrypt/{pw}")
public String crypt(#PathVariable String pw)
{
return BCrypt.hashpw(pw, "xxwv");
}
But now that I added those 3 dependencies, it keeps redirecting me to a login page that I never created. I just want to use the static hashing functions without Spring Boot adding random security I never asked for.
Adding following exclude parameter to the annotation of my Application class solved the problem:
#SpringBootApplication(exclude = { SecurityAutoConfiguration.class })

Session Management with Spring boot + REST using simple Map

I am developing the backend of an Angular + REST APIs application, the application needs some sort of session management (user is authenticated first using OTP then I need to keep track of any of his subsequent requests) ... I saw two examples for managing session with REST , first is using JWT + OAuth2 which I think is somehow over engineered as OAuth2 is not designed as I understand to be used within the same application (resource and authorization server are both within the same application) ... the other example uses redis and I can't introduce it to my current application ... actually what I need is something simple as storing the session in a static map-like structure that I always refer to (and moreover it would be nice to update the token with every client call to the backend, same like OAuth2 but simpler) ... I also checked the spring boot dependencies concerning sessions, all I found name external resource to be included like
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-hazelcast</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-mongodb</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
And I have a restriction not to add any external caching dependency like hazelcast ... also for a jdbc-session management, it will really affect performance to go to database with every client call

Categories