Home > Java > javaTutorial > body text

How to quickly implement Spring security permission authentication management (detailed steps)

不言
Release: 2018-10-24 11:12:33
forward
9354 people have browsed it

The content of this article is about how to quickly implement Spring security permission authentication management (detailed steps). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Build the project

Let’s get started without further ado.

pom.xml:

The core dependencies of spring security are as follows

<!-- spring security -->
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-security</artifactid>
</dependency>
<!-- spring security data -->
<dependency>
<groupid>org.springframework.security</groupid>
<artifactid>spring-security-data</artifactid>
</dependency>
Copy after login

Configure the user main class: User and implement security’s UserDetails

How to quickly implement Spring security permission authentication management (detailed steps)

Override several of its methods. The default configuration is false and needs to be changed to true. How to quickly implement Spring security permission authentication management (detailed steps)

#Configure UserService to implement the UserDetailsService interface and override the loadUserByUsername method.

This method is used during security login. The queried information must include user information and role information.

How to quickly implement Spring security permission authentication management (detailed steps)

2. Test

Create UserController, since we configured .antMatchers("/login","/register above ") allows access to only these two interfaces when not logged in.

How to quickly implement Spring security permission authentication management (detailed steps)

When you are not logged in, accessing non-permitted interfaces will redirect you to login.
The effect is as follows:

How to quickly implement Spring security permission authentication management (detailed steps)

After successful login, access /users

How to quickly implement Spring security permission authentication management (detailed steps)

We have easily implemented login authorization, so that our API is protected.

If you need to specify the access permissions for different roles, just add an annotation to the Controller.

@PreAuthorize("hasRole('ROLE_ADMIN')")//Requires administrator identity

@PreAuthorize("hasRole('ROLE_USER')")//Requires user identity

3. Ending

Through the above brief description, we have completed the integration of Spring boot mybatis with Spring security.
Due to limited time, I will only give a brief description in the article. Visit my Github to view the complete Demo.

url: https://github.com/admin79/SecurityDemo

The above is the detailed content of How to quickly implement Spring security permission authentication management (detailed steps). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!