Configure user logon in spring secruity database mode

Source: Internet
Author: User

This article will share with you an article about configuring user logon in spring secruity database mode. I hope this article will be helpful for you to learn about spring secruity.

A few days ago, I learned how to configure the user password directly in XML and use the spring security logon example. This method is suitable for demonstration. In most cases, real projects use databases or LDAP for user management. therefore, we will continue to learn how to use the database to configure user logon. Based on the previous example, the main changes are as follows:

Add database-related jar packages
All jar packages used are as follows, which may be redundant.

Program code

The Code is as follows: Copy code

Antlr-2.7.7.jar
Aopalliance. jar
Commons-logging-1.1.jar
Jstl-1.1.2.jar
Mysql-connector-java-3.1.12-bin.jar
Spring-aop-3.2.4.RELEASE.jar
Spring-aspects-3.2.4.RELEASE.jar
Spring-beans-3.2.4.RELEASE.jar
Spring-context-3.2.4.RELEASE.jar
Spring-context-support-3.2.4.RELEASE.jar
Spring-core-3.2.4.RELEASE.jar
Spring-e-xpression-3.2.4.RELEASE.jar
Spring-jdbc-3.2.4.RELEASE.jar
Spring-security-config-3.1.4.RELEASE.jar
Spring-security-core-3.1.4.RELEASE.jar
Spring-security-web-3.1.4.RELEASE.jar
Spring-tx-3.2.4.RELEASE.jar
Spring-web-3.2.4.RELEASE.jar
Spring-webmvc-3.2.4.RELEASE.jar
Standard-1.1.2.jar

 

Create a database and insert Test Data
The database script is as follows:

Program code

The Code is as follows: Copy code

Create TABLE 'users '(
'User _ id' INT (10) unsigned not null,
'Username' VARCHAR (45) not null,
'Password' VARCHAR (45) not null,
'Enabled' tinyint (1) not null,
Primary key ('user _ id ')
) ENGINE = InnoDB default charset = utf8;

Create TABLE 'user _ roles '(
'User _ ROLE_ID 'INT (10) unsigned not null,
'User _ id' INT (10) unsigned not null,
'Authority 'VARCHAR (45) not null,
Primary key ('user _ ROLE_ID '),
KEY 'fk _ user_roles '('user _ id '),
CONSTRAINT 'fk _ user_roles 'foreign key ('user _ id') REFERENCES 'users' ('user _ id ')
) ENGINE = InnoDB default charset = utf8;

Insert INTO users (USER_ID, USERNAME, PASSWORD, ENABLED)
VALUES (100, 'yihaomen', '123', TRUE );

Insert INTO user_roles (USER_ROLE_ID, USER_ID, AUTHORITY)
VALUES (1,100, 'Role _ user ');

 

In this way, the users table and user_roles table are created in database security, and records are inserted.

Configure the database authentication method for spring securtiy


Program code

The Code is as follows: Copy code

<Authentication-manager>
<Authentication-provider>
<Jdbc-user-service data-source-ref = "dataSource"
Users-by-username-query ="
Select username, password, enabled
From users where USERNAME =? "
Authorities-by-username-query ="
Select u. username, ur. authority from users u, user_roles ur
Where u. user_id = ur. user_id and u. username =? "
/>
</Authentication-provider>
</Authentication-manager>

 

Start the program and run the following results,


Program code download, remove the jar package, add your own, complete instance download: http://file.bKjia. c0m/download/2013/12/a35.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.