Eureka authentication method and UI - java

Is there any authentication method (user and password) and UI for it avalible for Netflix's server registry library Eureka? I'm looking for something like Spring Boot Admin's login page.

There is ui page which you can see just enter your host:port in browser. But it just shows info about registered clients, nothing you can do more. And for authentication you can use basic spring security. If you enable basic security with a user name and password, that ui asks you for it when you try to get to page. Additionally you should supply those username and password for registering clients too.

Just like any other Spring Boot Application:
#SpringBootApplication
#EnableEurekaServer
public class ServeurCloudEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ServeurCloudEurekaApplication.class, args);
}
}
In your pom file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Start your App and enter your host:port in browser

Related

Timestamp of restart of spring application

I have a microservice based on spring boot. I have a spring cloud configserver as well in-place. So depending upon the changes in the configserver microservice restart automatically.
So now i need to grab the timestamp when the application got restarted.
Is it possible to do so in the same microservice?
You can listen to spring events such as the ContextStartedEvent. There's a tutorial here that lays it out:
#EventListener
public void handleContextStartedEvent(ContextStartedEvent ctxStartEvt) {
System.out.println("Context Start Event received.");
}
yes, possible by adding the actuator and enabling the startup endpoint.
step1: Add the below dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.5.4</version>
</dependency>
step2: Add the below property into the application.properties files.
management.endpoints.web.exposure.include=startup
step3: Setup a little amount of buffer memory to store the startup info and it won't affect the performance at anywhere in your application.
#SpringBootApplication
public class StartupTrackingApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(StartupTrackingApplication.class);
app.setApplicationStartup(new BufferingApplicationStartup(2048));
app.run(args);
}
}
step: Hit the below URL using CURL like below or a POST call from POSTMAN. Then you will be seeing all startup-related info like when the startup started and ended. e.t.c.
curl 'http://localhost:8080/actuator/startup' -X POST | jq
For more explanation, view reference
In the response json, node "timeline"."startTime" is the exact startup timestamp when the applicant has started its startup job.

Secure a Java web app using the Spring Boot Starter for Azure Active Directory JWT token algorithm problem

I'm created java web application using spring boot starter for azure active directory step by step like is described in:https://learn.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-active-directory
My application with my azure account work fine when i open localhost:8080 it redirects me to azure where I do the login and then I'm redirected back to my app.
Problem is when i try to configure this dummy app with azure AD account from my customer. Here also when i open my app host app redirects me to azure login and after login i got error like in screenshot
and here is my application.properties
azure.activedirectory.tenant-id=placeholder
azure.activedirectory.client-id=placeholder
azure.activedirectory.client-secret=placeholder
azure.activedirectory.object-id=placeholder
azure.activedirectory.user-group.allowed-groups=group1
azure.activedirectory.session-stateless=true
security.oauth2.authorization.token-key-access=permitAll()
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
application.baseurl.logout.redirect=https://mydomain:8081/
application.groups.for.displaying=
application.groups.for.filtering=
server.port=8081
server.ssl.enabled=true
server.ssl.trust-store=/apps/tomcat/conf/trusted.jks
server.ssl.trust-store-password=mykeys
server.ssl.key-store=/apps/tomcat/conf/.keystore
server.ssl.key-store-password=f213495a0be855c4ab190a1f84cc18cd
server.ssl.key-store-type=JKS
server.ssl.key-alias=key-dev-ui
server.ssl.key-password=f213495a0be855c4ab190a1f84cc18cd
my configuration:
#Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable().authorizeRequests().anyRequest().authenticated().and().oauth2Login().and().csrf()
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and().oauth2Client();
}
Please check below points:
The issue may arise when the issuer value obtained from token is from different endpoint (v2) than expected.Please make sure to use latest version of spring boot api and check the same to be in dependencies.
You need to set the redirect URL as http://localhost:8080/login/oauth2/code/azure or http://localhost:8080/login/oauth2/code/ in the portal.You can configure other value according to your app in place of localhost:8080 .This redirect uri must be configured in your application properties.
Make sure to expose api and make sure the permissions are also configured and granted admin consent.
. Give default scope (make sure to add the scope in code)or directly give the scopes present in the app (check in app code) such as User.read ,file.read or offline_access and provide delegated permsissions in portal like below(if those are present in code ).
(or)
and grand admin consent
Also see springboot starter dev guide | ms docs and please check references below.
You may provide other configuration details by editing the question if above are not the cases to investigate further.
References:
spring-rest-azure-ad-oauth
using-spring-security-with-azure-active-directory-mga

How to enable H2 Database Server Mode in Spring Boot

I'm using a H2 database with a file using Spring Boot.
In my application.properties, I have this entry:
spring.datasource.url=jdbc:h2:file:c:/Testprojekte/spring-boot-h2-db
But now I would like to be able to look at the database while running the application, which currently isn't possible because I need to have the database running in server mode in order to do so. In the documentation I found that I have to add AUTO_SERVER=TRUE to the URL but this doesn't solve the problem.
So, what do I have to change to be able to connect to that database from different processes at the same time ?
thanks for any help!
Thorsten
You can start the H2 TCP server as a bean:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<!-- <scope>runtime</scope> -->
</dependency>
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean(initMethod = "start", destroyMethod = "stop")
public Server h2Server() throws SQLException {
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");
}
}
Then connect to it from your IDE with the following params (password - empty):
url: jdbc:h2:tcp://localhost:9092/mem:testdb
user: sa
More info is here and here.
You can enable h2 web console to access your h2 in memory or in file database using a web interface in your browser.
therefor add in application.properties the lines:
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
after that restart your spring boot application and check http://localhost:8080/h2-console with your browser.

No redirection to the app with Spring Boot + OAuth2 + Gitlab authentication

I am trying to add authentication layer to my app. It should use OAuth2 provided by our local Gitlab.
I have registered the app on Gitlab, so I get the security.oauth2.client.clientId and security.oauth2.client.clientSecret from there. I have set the Redirect URI to http://localhost:8080/index.html.
Now for the Spring application.properties, the OAuth2 part looks like this:
security.oauth2.client.clientId=SOME_ID
security.oauth2.client.clientSecret=SOME_SECRET
security.oauth2.client.accessToken=https://gitlab.ourserver.com/oauth/access_token
security.oauth2.client.userAuthorizationUri=https://gitlab.ourserver.com/users/sign_in
security.oauth2.client.tokenName=oauth_token
security.oauth2.client.authenticationScheme=query
security.oauth2.client.clientAuthenticationScheme=form
security.oauth2.resource.userInfoUri=userInfoUri: https://gitlab.ourserver.com/api/v4/user
The main application class is annotated with:
#SpringBootApplication
#EnableOAuth2Sso
public class GeneratorApplication
That is all for the configuration.
When I go to http://localhost:8080/index.html it is redirecting to the Gitlab login page, but after logging in it stays on the gitlab page...
Have you tried redirect-uri ?
spring.security.oauth2.client.registration.my-client-1.redirect-uri-template
Please check the official spring security documentation

How to activate JMX monitoring in spring boot standalone app

I went via almost all docs and all but not able to get grip on this mysterious stuff.
so my question - Can I use my standalone spring boot app to monitor health and other metrics of my app via http jmx url? Do I need to configure something else for this?
I have added below dependency in boot app.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
I have also configured below properties in my config file.
management.endpoints.web.exposure.include=*
management.endpoints.jmx.unique-names=true
management.server.port=8080
management.server.ssl.enabled=false
When I try to hit URL : http://localhost:8080/actuator/jolokia/health I am not able to see any results.
Also tried adding custom end point like below but not working.
#Endpoint(id="mypoint")
#Component
public class myPointEndPoint {
#ReadOperation
public String mypoint(){
return "Hello" ;
}
with additional property
management.endpoint.mypoint.enabled=true
The problem is the url you are trying to invoke.
First, retrieve the possible mbeans with: http://localhost:8080/actuator/jolokia/list
When you take a look at the Health mbean, you must provide the unique name and the operation (op).
In my case, it looked like: http://localhost:8080/actuator/jolokia/exec/org.springframework.boot:type=Endpoint,name=Health,identity=4200098/health
Also check the Jolokia documentation: https://jolokia.org/reference/html/index.html

Categories

Resources