Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Realm in Shiro

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Realm in Shiro". In daily operation, I believe many people have doubts about how to use Realm in Shiro. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Realm in Shiro". Next, please follow the editor to study!

Inheritance relationship of Realm

By looking at the inheritance relationships of classes, we find that there are actually many subclasses of Realm. Let's take a look at a few representative ones:

1.IniRealm

Maybe we don't know, but in fact this class was already used in our second article. This class starts with the following two lines of definition:

Public static final String USERS_SECTION_NAME = "users"

Public static final String ROLES_SECTION_NAME = "roles"

These two lines of configuration indicate that in the shiro.ini file, the user name and password of the table have roles under [users], and the corresponding relationship between roles and permissions is shown under [roles].

2.PropertiesRealm

PropertiesRealm provides another way to define users and roles, as follows:

User.user1=password,role1 role.role1=permission1

3.JdbcRealm

As the name implies, it is to query the user's role, permissions and other information from the database. When we open the JdbcRealm class, we see the following lines in the source code:

Protected static final String DEFAULT_AUTHENTICATION_QUERY = "select password from users where username =?"

Protected static final String DEFAULT_SALTED_AUTHENTICATION_QUERY = "select password, password_salt from users where username =?"

Protected static final String DEFAULT_USER_ROLES_QUERY = "select role_name from user_roles where username =?"

Protected static final String DEFAULT_PERMISSIONS_QUERY = "select permission from roles_permissions where role_name =?"

From the preset SQL of these lines, we can roughly infer the names and fields of the tables in the database. Of course, we can also customize the SQL. JdbcRealm is actually a subclass of AuthenticatingRealm, and we'll talk about AuthenticatingRealm in more detail later, so let's not expand it here. Next, let's talk about this JdbcRealm in detail.

JdbcRealm

1. Preparatory work

Using JdbcRealm involves database operations and database connection pooling. Here I use Druid database connection pooling, so first add the following dependencies:

Com.alibaba

Druid

RELEASE

Mysql

Mysql-connector-java

5.1.27

two。 Database creation

To use JdbcRealm, I first create a database. According to the preset SQL in JdbcRealm, the database table structure I define is as follows:

P309

Here, in order for you to see the relationship of the table intuitively, I use foreign keys. In actual work, it depends on the situation. Then add several pieces of test data to the table. Database script buddies can be downloaded to (https://github.com/lenve/shiroSamples/blob/v4/shiroDemo.sql) on github.

3. Configuration file processing

Then comment out all the configurations in shiro.ini and add the following comments:

JdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm

DataSource=com.alibaba.druid.pool.DruidDataSource

DataSource.driverClassName=com.mysql.jdbc.Driver

DataSource.url=jdbc:mysql://localhost:3306/shiroDemo

DataSource.username=root

DataSource.password=123

JdbcRealm.dataSource=$dataSource

JdbcRealm.permissionsLookupEnabled=true

SecurityManager.realms=$jdbcRealm

The configuration files here are very simple and do not go into too much detail. The only thing that partners need to note is that permissionsLookupEnabled needs to be set to true, otherwise JdbcRealm will not query permissions and user permissions.

4. test

OK, after completing the above steps, you can test it in the same way as in the second article, we can test the user login, user role and user permissions.

5. Custom query SQL

Friends understand the above, there is no problem with the custom query SQL. Let me give a simple example here. For example, I want to customize the corresponding SQL of the authenticationQuery pair and look at the JdbcRealm source code. We find that the SQL corresponding to authenticationQuery is originally select password from users where username =?. If you need to modify it, for example, my table name is not users but employee, then add the following configuration to shiro.ini:

JdbcRealm.authenticationQuery=select password from employee where username =? At this point, the study on "how to use Realm in Shiro" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report