Snowflake OAuth with Spring boot Java Application - java

I have a Spring boot Java application (with Snowflake DB) which has React Js inside same JAR package. Now i want to use Snowflake OAuth for Single SIgn on.
As per Snowflake documentation, i created security integration and configured it in application.properties file
https://docs.snowflake.com/en/user-guide/oauth-custom.html
security.oauth2.client.client-id=AZTI********LU
security.oauth2.client.user-authorization-uri=https://***.us-east-1.snowflakecomputing.com/oauth/authorize
security.oauth2.client.access-token-uri=https://***.us-east-1.snowflakecomputing.com/oauth/token-request
security.oauth2.resource.user-info-uri=http://localhost:9001/
security.oauth2.client.registered-redirect-uri=http://localhost:9001/api/caller1
Now I am confused on below points:
maven dependency to be used? and which annotation to be defined in Controller class to make use of OAuth SSO.
how to get user related info in registered-redirect-uri=http://localhost:9001/api/caller1?
do i need to create this API call in Controller? http://localhost:9001/login
Spring boot version=2.7.4, Java=11

Related

Prefix all endpoints with "/api" or another prefix, in spring reactive web (webflux)

I have a set of RestControllers in a spring reactive web project and I want to prefix all of the controllers with "api" or "test-api" in different environments.
I have tried to use server.servlet.context-path=/api and it's not working with spring reactive web (webflux) running on Netty server
The following property was added in spring boot 2.3 to achieve this with webflux
spring.webflux.base-path
Release Notes: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#configurable-base-path-for-webflux-applications
Have you tried using a placeholders in #RequestMapping, like for example #RequestMapping("${foo.bar}") ?
Thank you

How to disable write operation for Jolokia in Spring boot?

I have a Spring boot (v1.5.9) based application with Jolokia provided by Spring boot actuator.
Jolokia works fine. I can read values, for example:
http://localhost:8080/jolokia/read/java.lang:type=ClassLoading/Verbose
gives me:
{"request":{"mbean":"java.lang:type=ClassLoading","attribute":"Verbose","type":"read"},"value":false,"timestamp":1527859447,"status":200}
What I want is to disable writing operations, for example:
http://localhost:8080/jolokia/write/java.lang:type=ClassLoading/Verbose/true
The spring boot configuration looks like this:
management.security.enabled=false
management.endpoints.jmx.exposure.exclude=*
management.endpoints.web.exposure.include=jolokia,metrics
management.endpoint.jolokia.config.policyLocation=classpath:/jolokia.xml
And the Jolokia's policy in WEB-INF\classes\jolokia.xml (in resulting war, according to https://jolokia.org/reference/html/security.html) contains:
<restrict>
<commands>
<command>read</command>
<command>list</command>
<command>version</command>
<command>search</command>
</commands>
</restrict>
Despite this I see the following note in application's log:
jolokia: No access restrictor found, access to any MBean is allowed
And the write operation from the example above is working fine.
What I'm doing wrong? Should I put the policy file somewhere else? Is it possible to configure Jolokia's policy directly from the Spring boot configuration?
It looks like you've inadvertently used Spring Boot 2.0 configuration properties with Spring Boot 1.5.x. In 1.5 you should use jolokia.config.policyLocation. There's a little more information in the reference documentation.

Spring Cloud Config server inside a Spring Boot app with MVC

I'm inserting a Spring Cloud Config server directly into my existing Spring Boot App, by adding the module dependency and #EnableConfigServer. Everything works as expected except I just realized that the config server URL mapping is hijacking some of my existing API endpoints due to they are sharing the same server.port
For example, I had an existing page at v1/docs/index.html, and this will be now mapped automatically to org.springframework.cloud.config.server.environment.EnvironmentController#labelled which has
#RequestMapping("/{name}/{profiles}/{label:.*}")
public Environment labelled(#PathVariable String name, #PathVariable String profiles,
Wondering is there anyway I can separate config server to a different port? Or add a special prefix to it to avoid URL mapping conflicts? Or given the current configurability it's just a bad idea to utilizing existing API servers as a config server?
Found the answer I need after reading the document more carefully:)
https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_spring_cloud_config_server
To change the location of the server endpoints you can (optionally) set spring.cloud.config.server.prefix, e.g. "/config", to serve the resources under a prefix. The prefix should start but not end with a "/". It is applied to the #RequestMappings in the Config Server (i.e. underneath the Spring Boot prefixes server.servletPath and server.contextPath).

Properly (and easily ?) initiate a Spring MVC Project

What's the best method to initiate a new Spring Project ?
Initiating Spring project is a lot of pain with various xml and db configuration.
Is there an "official" repository with complete sample project, as MVC with db access and so ?
Spring Boot may also be the solution, but some point still not clear to me :
How to add external components (such as Quartz) without creating some xml config ? (No such xml in Boot apparently)
Is a Spring Boot builded app is production-proof ?
As writen in the comments http://start.spring.io/ is the best way to start Spring boot project(STS has integration for it).
If you want to use something that is not supported by Spring Boot you can init spring beans the same way you do it in xml, just use java configuration. See this for example: http://www.tutorialspoint.com/spring/spring_java_based_configuration.htm
Also useing xml is still available. You can add #ImportResource on your Configuration class
#EnableAutoConfiguration
#Configuration
#ImportResource({"classpath*:applicationContext.xml"})

Grails app using services from spring-mvc backend

We have a fairly involved web application written using spring-mvc with a maven build system and would like to harness all the power of Grails for the front end.
So the Grails app will essentially call into the spring-mvc app's service layer to access its business logic and data.
I need some guidance with my architectural approach to this integration at a high level.
From my understanding, I will need to;
- add my spring-mvc app as a compile dependency in my BuildConfig.groovy.
- Expose the service layer objects as service beans in my conf/spring/resources.groovy and inject them into my controllers
Questions:
My spring-mvc app has lots of dependencies of its own (which it obviously has to have) which are causing lots of dependency errors. Should I be setting "transitive=false" in my config and calling all of these in my Grails app?
How should the datasource get configured? I guess I have to integrate the applicationContext of my spring-mvc app by calling it from my Grails applicationContext and hope it all bootstraps nicely?
So the Grails app will essentially call into the spring-mvc app's service layer to access its business logic and data
Can you be a bit more specific about which components of the Spring MVC you want to use from Grails, is it just the services and datasource?
I will need to add my spring-mvc app as a compile dependency in my BuildConfig.groovy
yes
Expose the service layer objects as service beans in my conf/spring/resources.groovy
Although you could make the Spring beans known to your Grails app by defining them individually in resources.groovy, this is unnecessary because you've already defined them in an Spring XML file (presumably) in the Spring MVC project.
Instead you can use the importBeans method of the BeanBuilder to import the Spring beans defined in this XML file into the Grails app. Once you've added the Spring MVC project as a dependency of your Grails app, the Spring XML file should be on your classpath, so all you need to do is add the following to resources.groovy
beans = {
importBeans('classpath:/path/to/file/applicationContext-services.xml')
}
How should the datasource get configured?
A Spring bean named dataSource defines the datasource that a Grails app uses. In a standard Grails app, this bean is created based on the configuration in DataSource.groovy. If your Spring MVC app defines a bean with this name, then this should be used instead after making the changes above. To be sure that Grails is using the datasource from your Spring MVC app rather than whatever is in DataSource.groovy, I guess you could delete the contents of the latter.

Categories