H2 database: h2 console not working with spring-boot - spring-boot

I followed the next tutorial for h2 implementation. H2 works well, but the console isn't - the page isn't opened.
I tried many gifts, so my final application.properties looks so:
#Database settings
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.data.jpa.repositories.bootstrap-mode=default
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true
spring.datasource.generate-unique-name = false
spring.h2.console.path=/h2-console/
spring.jpa.hibernate.ddl-auto=update
spring.datasource.tomcat.connection-properties=useUnicode=true;characterEncoding=utf-8;
spring.datasource.sql-script-encoding=UTF-8
spring.jpa.open-in-view=false
spring.datasource.initialization-mode=always
#swagger
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER
#Disable thymeleaf cashing
spring.template.cache = false;
dependencies:
ext {
spring_boot_version = '2.6.6'
}
implementation "org.springframework.boot:spring-boot-starter-parent:${spring_boot_version}"
implementation "org.springframework.boot:spring-boot-starter-data-jpa:${spring_boot_version}"
implementation "org.springframework.boot:spring-boot-starter-validation:${spring_boot_version}"
implementation "org.springframework.boot:spring-boot-starter-thymeleaf:${spring_boot_version}"
implementation "org.springframework.boot:spring-boot-starter-web:${spring_boot_version}"
runtimeOnly "com.h2database:h2:1.4.193"
....
The uri I try to open:
http://localhost:8080/h2-console

Try this:
server.port=8080
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE;

Could you try to add the following configuration to the security config?
http.authorizeRequests()
.antMatchers("/h2-console/**").permitAll()
Ideally, you need to config (depending on the spring boot version) to allow access to /h2-console URL.
You can also publicise your security configuration so that I can have a look.

Related

Where to specify the driver version db h2?

there are two databases h2 of different versions: 1.4.200 and 2.1.214.
contents of the application.yaml file:
app:
datasource1:
jdbcUrl: jdbc:h2:~/database/test1
driverClassname: org.h2.Driver
h2.version: 1.4.200
username: xxx
password: xxx
datasource2:
jdbcUrl: jdbc:h2:~/database/test2
driverClassname: org.h2.Driver
h2.version: 2.1.214
username: xxx
password: xxx
there are two configuration classes: DataSourcesConfiguration1 и DataSourcesConfiguration2.
#Configuration
#EnableTransactionManagement
public class DataSourcesConfiguration1 {
#Primary
#Bean
#ConfigurationProperties("app.datasource1")
public DataSourceProperties dataSourceProperties1() {
return new DataSourceProperties();
}
#Primary
#Bean("dataSource1")
#ConfigurationProperties(prefix = "app.datasource1")
public HikariDataSource dataSource1() {
return dataSourceProperties1()
.initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
#Primary
#Bean("jdbcTemplate1")
public NamedParameterJdbcTemplate jdbcTemplate1()
{
HikariDataSource ds = dataSource1();
return new NamedParameterJdbcTemplate(ds);
}
#Primary
#Bean
#Qualifier("transactionManager1")
DataSourceTransactionManager transactionManager1() {
HikariDataSource ds = dataSource1();
return new DataSourceTransactionManager(ds);
}
}
the DataSourcesConfiguration2 class looks similar, without the #Primary annotation.
I do not know where to specify the db driver version.
I can only specify the version in the file pom.xml, but it works for two data sources:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<!-- <version>2.1.214</version> -->
<scope>runtime</scope>
</dependency>
lines
h2.version: 1.4.200 and h2.version: 2.1.214
specified in the file application.yaml, ignored when creating beans.
need to find some property HikariDataSource ds.setDriverVersion();
but I don't know where to specify the driver version db h2 ?
You cannot load two versions of H2 at once, unless they are loaded by different classloaders.
H2 1.4.200 is an old unsupported version, why you need to use it these days? If you want to move your data from old version of H2 to new one you can try to use org.h2.tools.Upgrade class in H2 2.1.214 or you can use a third-party upgrade tool: https://github.com/manticore-projects/H2MigrationTool
But if you really need to use them both, the simplest solution is to include H2 2.1.214 to the classpath of your application and start a separate H2 Server process with H2 1.4.200 (java -jar h2-1.4.200.jar), connection URLs will be jdbc:h2:tcp://localhost/~/database/test1 for 1.4.200 and the same jdbc:h2:~/database/test2 for 2.1.214.
Your application will use a remote database on 1.4.200 server and embedded database opened by 2.1.214.

SEO user friendly url using Pretty faces and spring boot

I have followed this to configure spring boot work with JSF and the configuration is ok however I need to get rid of .xhtml suffix in all my jsf using pretty faces. I have so far managed to configure PrettyFaces RewriteFilter in my spring boot and added the URLMapping in my managed bean but it doesnt seem to work am getting 'Whitelabel Error Page'. Here is my login managed bean
#Scope(value = "session")
#Component(value = "loginMgr")
#URLMapping(id = "login",
pattern = "/login",
viewId = "/my_context/login.xhtml")
public class LoginManager {
}
and my pretty faces configuration bean
#Bean
public FilterRegistrationBean prettyFilter() {
System.out.println("pretty filter called");
RewriteFilter filter=new RewriteFilter();
FilterRegistrationBean prettyFilter = new FilterRegistrationBean(filter);
prettyFilter.setDispatcherTypes(DispatcherType.FORWARD, DispatcherType.REQUEST,
DispatcherType.ASYNC, DispatcherType.ERROR);
prettyFilter.addUrlPatterns("/*");
return prettyFilter;
}
and this is my sping boot application.properties
spring.datasource.url= jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=password
#spring.jpa.show-sql=true
spring.datasource.driver-class-name=org.postgresql.Driver
#spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
server.context-path=/my_context
spring.security.enabled=false
management.security.enabled=false
security.basic.enabled=false
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrateg
I am using jsf 2.2 and primefaces 5.3 and below are the pretty faces maven dependency
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-servlet</artifactId>
<version>3.4.1.Final</version>
</dependency>
<dependency>
<groupId>org.ocpsoft.rewrite</groupId>
<artifactId>rewrite-integration-faces</artifactId>
<version>3.4.1.Final</version>
</dependency>
and this
is the error am getting
If you would like to get rid of the .xhtml suffix and use MyFaces 2.3, you can simply set:
org.apache.myfaces.AUTOMATIC_EXTENSIONLESS_MAPPING to true.
So you dont need PrettyFaces or Rewrite.
NOTE:
it might throw a exception that #getServletRegistrations can't be called from a Listener not configured in web.xml or web-fragment.xml.
In this case you need to add org.apache.myfaces.webapp.StartupServletContextListener to your web.xml or web-fragment.xml..

Keycloak Policy Enforcing

Application.properties:
server.port=8180
keycloak.realm = finaltest
keycloak.auth-server-url = http://localhost:8080/auth
keycloak.resource = ex
keycloak.public-client=false
keycloak.enabled=true
keycloak.credentials.secret=secret
keycloak.ssl-required = external
keycloak.cors=true
keycloak.use-resource-role-mappings=true
keycloak.security-constraints[0].auth-roles[0]=master
keycloak.security-constraints[0].security-collections[0].patterns[0]=/*
keycloak.policy-enforcer-config.enforcement-mode=ENFORCING
keycloak.policy-enforcer-config.lazy-load-paths=true
RESOURCES:
I have two resources namely
http://localhost:8180/flights.html
http://localhost:8180/hotels.html
I have protected these using the policies in keycloak admin console.How do I enforce these policies in the application?
keycloak.policy-enforcer-config.enforcement-mode=ENFORCING this line will enforce policies.But you must have spring boot version 2.0 and above.

Spring Cloud Config Server issue - Configuring multiple sources native and jdbc

I want to connect to multiple repositories i.e native (file system) and jdbc in spring cloud config. I created a spring cloud config server with below details
application.properties
server.port=8888
spring.profiles.include=native,jdbc
spring.cloud.config.server.native.search-locations=classpath:/config,classpath:/app1, classpath:/app2,classpath:/ep
encrypt.key=abcdef
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/configuration?useSSL=false
spring.cloud.config.server.jdbc.sql=SELECT properties.key, properties.value from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
spring.datasource.username=root
spring.datasource.password=root
spring.cloud.config.server.native.order=1
spring.cloud.config.server.jdbc.order=2
Irrespective of priority order it always fetches information from jdbc and not from native.
I tried adding the last 2 properties for order to bootstrap.properties still same behavior.
Am is missing anything ? Is my configuration correct ? Please suggest
in spring boostrap.yml loaded before application.yml so you declare server port,config search location and active profile configuration is good approach for this stack,so keep it simple boostrap.yml also spring cloud default profile is native
and in application-"profile".yml is have environment and other configuration properties
and your boostrap.yml or properites like that
server:
port: 8888
spring:
application:
name: appName
profiles:
active: native,jdbc
cloud:
config:
server:
native:
order: 1
searchLocations: classpath:/config,classpath:/app1, classpath:/app2,classpath:/ep
and create applicaiton-jdbc.properties or yml file in same layer in boostrap.yml or properties and declare jdbc properties
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: 'jdbc:mysql://localhost:3306/configuration?useSSL=false'
cloud:
config:
server:
jdbc:
order: 2
sql: 'SELECT properties.key, properties.value from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?'
username: root
password: root
and your config server configuration like this
#SpringBootApplication
#EnableConfigServer
#Import({JdbcEnvironmentRepository.class})
public class ConfigServer {
#ConfigurationProperties(prefix = "spring.datasource")
#Bean
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
#Bean
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(dataSource());
}
public static void main(String[] arguments) {
SpringApplication.run(ConfigServer.class, arguments);
}
}

Using Spring Cloud Connectors together with HikariCP

I would like to use HikariCP from the Spring Cloud Connectors. I am not sure how to proceed...
I have updated my Spring Cloud Connectors to 1.2.0.RC1.
Here is my current config:
#Configuration
#Profile({ Profiles.CLOUD })
public class CloudDataSourceConfiguration extends AbstractCloudConfig {
#Bean
public DataSource dataSource() {
int dbcpMaxActive = 10;
int dbcpMaxWait = 200;
PoolConfig poolConfig = new PoolConfig(dbcpMaxActive, dbcpMaxWait);
ConnectionConfig connectionConfig = new ConnectionConfig("sessionVariables=sql_mode='ANSI';characterEncoding=UTF-8");
DataSourceConfig serviceConfig = new DataSourceConfig(poolConfig, connectionConfig);
return connectionFactory().dataSource("CLEARDB_DATABASE", serviceConfig);
}
}
Can someone please advise?
edit: When I start the app with the cloud profile, I can read
2015-05-23 22:46:56,029 [localhost-startStop-1] INFO org.springframework.cloud.service.relational.PooledDataSourceCreator - Found Tomcat high-performance connection pool on the classpath. Using it for DataSource connection pooling.
from the log output.
edit 2: HikariCP is in the classpath and it seems that tomcat high performance connection pool is also in the classpath.
As stated in my second edit, both tomcat jdbc & HikariCP were on the classpath. By removing tomcat jdbc as follows (in my gradle script):
compile("org.springframework.boot:spring-boot-starter-data-jpa"){
exclude group: 'org.apache.tomcat', module: 'tomcat-jdbc'
}
only HikariCP remained on the classpath and it was picked up properly as shown by the log output below:
INFO org.springframework.cloud.service.relational.PooledDataSourceCreator - Found HikariCP on the classpath. Using it for DataSource connection pooling.

Resources