Chapter 1 Introduction to Shiro-"mongoshiro", mongoshiro

Source: Internet
Author: User

Chapter 1 Introduction to Shiro-"mongoshiro", mongoshiro
1.1 Introduction

Apache Shiro is a Java security framework. At present, there are more and more people using Apache Shiro because it is quite simple. Compared with Spring Security, Spring Security may not have powerful functions, but it may not need to be so complicated in actual work, so it is enough to use a small and simple Shiro. For which one of them is better, you don't have to worry about it. You can solve the project problems more simply.

This tutorial only introduces the basic Shiro usage and does not analyze the source code too much.

 

Shiro can easily develop enough applications, which can be used not only in the JavaSE environment, but also in the JavaEE environment. Shiro can help us complete authentication, authorization, encryption, session management, Web Integration, caching, and so on. This is what we want, and Shiro's API is also very simple. Its basic functions are shown in:

Authentication:Identity Authentication/login to verify whether the user has the corresponding identity;

Authorization:Authorization, that is, permission verification, verifies whether an authenticated user has a certain permission, that is, to determine whether the user can do things. Common examples include verifying whether a user has a certain role. Or fine-grained verification of whether a user has a certain permission on a resource;

Session Manager:Session management, that is, after a user logs on, it is a session. Before the user exits, all its information is in the session. The session can be in a normal JavaSE environment, it can also be in a Web environment;

Cryptography:Encryption protects data security. For example, passwords are encrypted and stored in the database instead of plaintext;

Web Support:Web Support, which can be easily integrated into the Web environment;

Caching: cache. For example, after a user logs on, the user information and roles/permissions of the user do not need to be checked every time, which improves the efficiency;

Concurrency:Shiro supports concurrent verification of multi-threaded applications. If another thread is enabled in one thread, permissions can be automatically transmitted;

Testing:Provides test support;

Run:Allow one user to pretend to be another user (if they permit) for access;

Remember Me:Remember me, this is a very common feature, that is, after a login, you do not need to log on again next time.

 

Remember, ShiroUsers and permissions are not maintained. We need to design these items by ourselves/Provided, and then injected to Shiro through the corresponding interfaceYou can.

 

Next, let's take a look at Shiro's architecture from the external and internal perspectives. For a good framework, there should be very simple and easy-to-use APIs from the external perspective, and the API contract should be clear; from the internal perspective, it should have a scalable architecture, that is, it is very easy to insert User-Defined implementations, because no framework can meet all requirements.

 

First, let's look at Shiro externally, that is, how to use Shiro to complete the work from the application perspective. For example:

 

We can see that the object for direct application code interaction is Subject, that is, the core of Shiro's external API is Subject. The meaning of each API is as follows:

Subject:The Subject represents the current "user". This user is not necessarily a specific person. Anything that interacts with the current application is Subject, such as web crawlers and robots; it is an abstract concept. All Subject is bound to SecurityManager, and all interactions with Subject are delegated to SecurityManager. Subject can be considered as a facade, and SecurityManager is the actual executor;

SecurityManager:Security Manager; that is, all security-related operations will interact with SecurityManager; and it manages all Subject; it can be seen that it is the core of Shiro, it interacts with other components introduced later. If you have learned SpringMVC, You can regard it as the DispatcherServlet front-end controller;

Realm:Domain, Shiro obtains security data (such as users, roles, and permissions) from Realm, that is, SecurityManager needs to verify the user identity, then it needs to get the corresponding user from Realm for comparison to determine whether the user identity is legal; it also needs to get the corresponding role/permission of the user from Realm to verify whether the user can perform operations; we can regard Realm as a DataSource, that is, a secure data source.

 

That is to say, for us, the simplest Shiro application:

1. The application code is authenticated and authorized through Subject, and the Subject is delegated to SecurityManager;

2. We need to inject Realm into Shiro's SecurityManager so that SecurityManager can obtain valid users and their permissions for judgment.

 

From the above, we can see that ShiroNo maintenance user/Permission, but through RealmAllow developers to inject data on their own.

 

Next, let's look at Shiro's architecture from the perspective of Shiro, as shown in:


Subject:Subject, you can see that the subject can be any "user" that can interact with the application ";

SecurityManager:It is equivalent to DispatcherServlet in SpringMVC or FilterDispatcher in Struts2; it is the heart of Shiro; all specific interactions are controlled through SecurityManager; it manages all Subject and manages authentication and authorization, sessions, and caches.

Authenticator:The authenticator is responsible for subject Authentication. This is an extension. If you think Shiro is not good by default, you can customize it. Authentication Strategy is required ), that is, when the user authentication is passed;

Authrizer:The authorizer or access controller is used to determine whether the subject has the permission to perform corresponding operations, that is, to control which functions the user can access the application;

Realm:There can be one or more Realm, which can be considered as a secure entity data source, that is, used to obtain a security entity; it can be JDBC implementation, LDAP implementation, or memory implementation; provided by users. Note: Shiro does not know where your users/permissions are stored and in what format. Therefore, we generally need to implement our own Realm in applications;

SessionManager:If you have written Servlet, you should know the concept of Session. For a Session, someone needs to manage its lifecycle. This component is SessionManager. Shiro can be used not only in the Web environment, it can also be used in environments such as normal JavaSE environments and ejbs. In all cases, Shiro abstracts a Session to manage the interaction data between the subject and the application. In this way, for example, we use it in the Web environment. We started with a Web server, and then we mounted an EJB server. At this time, we wanted to put the session data of the two servers in one place, in this case, you can implement your own distributed Sessions (such as placing data on the Memcached server );

SessionDAO:DAO is used by everyone. Data access objects are used for Session CRUD. For example, if we want to save the Session to the database, we can implement our own SessionDAO and write it to the database through JDBC; for example, if you want to put the Session in Memcached, You can implement your own Memcached SessionDAO. In addition, you can use the Cache in SessionDAO to Cache the Session to improve performance;

CacheManager:The cache controller is used to manage caches such as users, roles, and permissions. Because the data is rarely changed, it can improve access performance after being put into the cache.

Cryptography:Password module, Shiro improves some common encryption components for encryption/decryption, such as passwords.

Now, we know the Shiro architecture and its components. Next, let's learn about the Shiro components.

Code Source: if you are interested in mingli, you can go to the ball ~ Sharing learning technologies: 2042849237

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.