Confusing about <security-role> in web.xml - java

I'm understand that
<security-role><role-name>Admin</role-name></security-role>
is for container map it with tomcat-users.xml (realm)
but I confuse about if I didn't use realm but I use database how container know which field in my database is for role-name or it have naming convention in database field name like "role_name" and container will know it
Thank you for every advices

Just use a database realm and configure the table and column names in a <Realm> element in server configuration file. For Tomcat, this is described in the Realm HOWTO. Here's an extract of relevance, from the JDBCRealm chapter:
Quick Start
To set up Tomcat to use JDBCRealm, you will need to follow these steps:
If you have not yet done so, create tables and columns in your
database that conform to the
requirements described above.
Configure a database username and password for use by Tomcat, that
has at least read only access to the
tables described above. (Tomcat will
never attempt to write to these
tables.)
Place a copy of the JDBC driver you will be using inside the
$CATALINA_HOME/lib directory. Note
that only JAR files are recognized!
Set up a <Realm> element, as described below, in your
$CATALINA_BASE/conf/server.xml file.
Restart Tomcat 6 if it is already running.
Realm Element Attributes
To configure JDBCRealm, you will
create a <Realm> element and nest it
in your $CATALINA_BASE/conf/server.xml
file, as described above. The
attributes for the JDBCRealm are
defined in the Realm configuration
documentation.
Example
An example SQL script to create the
needed tables might look something
like this (adapt the syntax as
required for your particular
database):
create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);
create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key(user_name, role_name)
);
Example Realm elements are included
(commented out) in the default
$CATALINA_BASE/conf/server.xml file.
Here's an example for using a MySQL
database called "authority",
configured with the tables described
above, and accessed with username
"dbuser" and password "dbpass":
<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser&password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>
Pretty clear, isn't it? If you already have a JDBC datasource configured in Tomcat (for connection pooling and on), then you can also use DataSourceRealm instead.
The tomcat-users.xml which you're talking about is by the way called UserDatabaseRealm.

Related

Spring Boot -- not running data.sql on Start

I'm trying to run a simple H2 Spring project. I've used starter.spring.io to initialize a project with the web, JDBC, JPA, andH2` dependencies. I'm following along with in28minutes Spring Mastercourse.
All I'm trying to do is initialize one table on startup. I do the following in my data.sql:
CREATE TABLE person
(
id integer not null,
name varchar(255) not null,
location varchar(255),
birth_date timestamp,
primary key(id)
);
My application.properties looks like this:
spring.h2.console.enabled=true
I haven't touched a single other file in the entire project. Just loaded a project, added those lines, and tried running it. For some reason, my table isn't being created. I've gone through the steps in the tutorial probably 15 times by this point and can't find what I'm doing wrong, any help would be greatly appreciated.
Edit: My JDBC URL is correct, as well as my driver class. I've checked those each time I tried re-running the steps
With your exact dependency setup, and with your schema, I can see the data in the web console.
The predefined Generic H2 (embedded) settings should provide the right values, but you should input the following :
Driver Class : org.h2.Driver
JDBC URL : jdbc:h2:mem:testdb
User Name : sa
Password : <empty>
on the web console login page, as those are the defaults for an embedded h2 database.
If you don't see any issue in the startup log, this is most likely it.
Take note that H2 will not error out if you try to connect to a non-existent database.
e.g. : jdbc:h2:mem:db, jdbc:h2:mem:foobar will not produce any errors and connect to empty databases.

How to configure multiple schema in the same datasource in Tomcat?

My application uses multiple schemas in a database (userName and password connecting to the database is same for all schema). Right now am configuring the datasource by providing resouces tag as mentioned below in tomcat server.xml
<Resource name="jdbc/datasource" auth="Container"
type="ConnectionPoolDataSource"
scope="Shareable" url="jdbc_url"
factory="JDBCObjectFactory"
driverClassName="JDBCDriver"
serverName="myserver.com"
libraries="Schema1"
userName="username" pwd="password"
secure="false"
prompt="false"/>
How can I modify it to accommodate multiple schema. My application uses DB2 database
Tomcat JDBC connection pool for different schema this provides a solution to prefix tables with schema name in each query but i want it to be configured in server.xml
Note: Tried to provide comma separated values in libraries (libraries="Schema1,Schema2") but tables from schema2 were not accessible in my application.

How to set MySQL ANSI_QUOTES mode via MySQL Java Connector?

I have a Scala (Java) Play Web Application where I wrap database identifiers with ANSI double-quotes in queries e.g.
select *
from "account"
where "deleted" is null
order by "account_name"
This is necessary because I use among others H2 in-memory and Postgres databases for deploying the application in different scenarios e.g. CI server. Now we need to deploy it also in MySQL and by default wrapping identifiers in double-quotes is not supported. However, following directions from this post mysql double-quoted table names I see we can set this session parameter and then it should work.
How can I set this session parameter while opening the connection through the MySQL Java Connector? My database URL looks like this: jdbc:mysql://odysseus:3306/idxsrs-trading?param=xxx
Mysql session variable in JDBC string
Using the above to link in combination with the link you've posted, I'd try this.
jdbc:mysql://localhost:3306/db?sessionVariables=sql_mode=ANSI_QUOTES

How to set default schema in a Spring/mybatis application with Oracle DB?

Coming from a mysql background, I am able to set the default schema name that I want to use for all my sql queries in the connection url. I now have an Oracle DB that I need to access. I am aware that I cannot specify the schema I want to use in the URL since the user is the schema name being used.
I realize that I can use a line of SQL code:
ALTER SESSION SET CURRENT_SCHEMA=default_schema
The project is using mybatis 2.3.5 as my SQL framework, but I am completely new to mybatis. Is there a simple way to configure mybatis to accomplish this? My application is a Spring 3 application, so I am using the Spring DataSourceTransactionManager to manage my transactions. I would presume that the manager must be made aware of this requirement to ensure that the command is sent whenever creating a new connection.
I've tried searching online, but most of the examples I find all have the schema names included within the sql queries in the SqlMaps, which I find to be bad practice.
In an ideal world, the schema name would be part of the URL such that I can make changes to the schema name for different environments (ex: dev, test, prod, etc) without touching the code (ie: only configured at the JNDI/application server level). I would be happy if I could use a Spring configuration value to set this as well as I could still use a JNDI lookup or a system environment property to retrieve the value.
Can anyone point me in the right direction?
Thanks,
Eric
As far as I know, there is no option in Oracle to change your URL in order to connect to a specific user schema.
1) mybatis: You may set your current schema to a deserved one before you start your operations. You can write your specification in a property file and set your method's arguments from that file. You do not need to change your code to change your schema in that case.
<update id="mySetSchemaMethod" parameterClass="String">
ALTER SESSION SET CURRENT_SCHEMA = ${schemaName}
</update>
2) trigger: If you are using this connection only for this particular java application, you can set a client event trigger so set your CURRENT_SCHEMA. This time, you need to change the trigger in order to manage test/prod changes.
CREATE OR REPLACE TRIGGER Set_Schema_On_Logon
AFTER LOGON
ON MY_SCHEMA
BEGIN
ALTER SESSION SET CURRENT_SCHEMA = MY_TEST_SCHEMA;
END;

Autocreate Spring Entity "authorities" during testing

When trying unit tests with Spring Security & Hibernate, none of the security entities "user" or "authorities" are being autocreated. What I have done so far is to write an "user" bo that triggers generation of the appropiate table. However, I am stuck with the authorities:
(as advised by http://java.dzone.com/articles/getting-started-spring for postgresql)
CREATE TABLE authorities
(
username character varying(50) NOT NULL,
authority character varying(50) NOT NULL,
CONSTRAINT fk_authorities_users FOREIGN KEY (username)
REFERENCES users (username) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
);
Question: With Hibernate/JPA2, what is the appropiate syntax in order to create a BO representing this query?
Question: Actually, I do not want to create the entry using my own BO. Any better way to make Spring Security or Hibernate create all required tables during test run?
Thanks
Set the hibernate property hibernate.hbm2ddl.auto to update, for example. This should let hibernate automatically create (and update) the tables in needs.
<property name="hibernate.hbm2ddl.auto" value="update" />
Actually, I do not want to create the entry using my own BO. Any better way to make Spring Security or Hibernate create all required tables during test run?
If you don't plan to use Hibernate to interact with these tables, it makes indeed little sense to have Entities for them.
My suggestion would thus be to place the Spring Security tables creation script in an import.sql file and to put this file on the root of the class path and Hibernate will automatically execute it after schema export. See Spring/Hibernate testing: Inserting test data after DDL creation for details (just put your DDL statements on a single line).
Thanks, Pascal, this is just what I have been looking for, however, it does not work. I use maven and put import.sql into the resources dir root (content: CREATE TABLE justatest (aaa character varying(50) NOT NULL );). I also set . Running mvn test copies import.sql to target dir... but nothing happens. logback[debug] does not mention import.sql at all. Any idea where I am going wrong? (Hibernate V 3.5.1-Final)
I'm using this feature with Maven and I cannot reproduce your problem. I have hbm2ddl.auto set to create, my import.sql file is in src/test/resources and it gets executed as expected at the end of the schema export when running tests. Here is the log entry I get (using logback):
20:44:37.949 [main] INFO o.h.tool.hbm2ddl.SchemaExport - Executing import script: /import.sql

Categories