Shiro of Jdbcrealm and authentication strategy

Source: Internet
Author: User

1. Use the Shiro framework to complete certification work, using Lnirealm by default. If you need to use a different realm, you need to make the relevant configuration.

The 2.lni configuration file explains: [Main]section is where you configure the application's SecurityManager instance and any of its dependent components (such as: Realms).

[Main]myrealm=cn.sxt.realm.myrealm# Dependency Injection Securitymanager.realm= $myRealm

[Users]section allows you to define a set of static user accounts. This is useful in environments where the majority of users have a few user accounts or user accounts that do not need to be dynamically created at runtime.

[Users]zhangsan=1111Lisi=2222, Role1,role2

[Roles]section allows you to associate roles and permissions defined in [Users]section]. In addition, this is useful in environments where the majority of users have a few user accounts or user accounts that do not need to be dynamically created at runtime.

[Users]zhangsan=1111, Role1[roles]role1=user:add,user:delete

3. Use Jdbcrealm to complete the identity verification.

By observing Jdbcrealm, we realize Jdbcreaml:

A) You need to set DataSource for Jdbcrealm

b) Apply the user table users in the database corresponding to the specified datasource, with fields such as username,password,password_salt in the table.

Implementation steps:

A) Create a new database table:

b) Configure the Shiro.ini file:

[main] #配置数据源dataSource=Com.mchange.v2.c3p0.ComboPooledDataSourcedataSource.driverClass=  COM.MYSQL.JDBC.DRIVERDATASOURCE.JDBCURL=jdbc:mysql://Localhost:3306/shiro datasource.user=Rootdatasource.password=1111jdbcrealm=  Org.apache.shiro.realm.jdbc.jdbcrealm#$ represents the Reference object Jdbcrealm.datasource=$dataSourcesecurityManager. Realm = $jdbcRealm

c) Guess

 Public Static voidMain (string[] args) {Factory<SecurityManager> factory =NewInisecuritymanagerfactory ("Classpath:shiro.ini"); SecurityManager SecurityManager=factory.getinstance ();        Securityutils.setsecuritymanager (SecurityManager); Subject Subject=Securityutils.getsubject (); Usernamepasswordtoken token=NewUsernamepasswordtoken ("Wangwu","1111"); Try{subject.login (token); if(subject.isauthenticated ()) {System. out. println ("Validation by"); }        } Catch(authenticationexception e) {System. out. println ("validation Failed"); }    }

5. Authentication strategy: Authentication strategy, there are 3 kinds of authentication strategies in Shiro;

A) Atleastonesuccessfulstrategy: if one (or more) Realm proves successful, the overall attempt is considered successful. If no validation succeeds, the overall attempt fails.

b) Firstsuccessfulstrategy: As long as there is a successfully verified realm returned information will be used. All further realms will be ignored. If no validation succeeds, the overall attempt fails.

c) Allsucessfulstrategy: For the overall attempt to succeed, all the configured realms must be validated successfully. If no validation succeeds, the overall attempt fails.

The default policy is: Atleastonesuccessfulstrategy

6. Set the authentication policy:

#验证策略设置authenticationStrategy=  Org.apache.shiro.authc.pam.FirstSuccessfulStrategysecurityManager.authenticator.authenticationStrategy=$ Authenticationstrategy

Shiro of Jdbcrealm and authentication strategy

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.