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 customize user information in Spring security

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to customize user information in Spring security. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Implementation steps

1. Copy the source code of the previous example

Rename the package name case1 to case2

Rename Case1Application.java to Case2Application.java

two。 Custom security configuration

Create a new config package and create a new WebSecurityConfig.java class.

The complete code is as follows:

Package net.txt100.learn.springsecurity.base.case2.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.crypto.password.PasswordEncoder / * Title: WebSecurityConfig * Package: net.txt100.learn.springsecurity.base.case2.config * Creation date: 2019-08-11 * Description: * * @ author Tonglei * @ since 1.0 * / @ Configurationpublic class WebSecurityConfig extends WebSecurityConfigurerAdapter {@ Bean public PasswordEncoder passwordEncoder () {/ / configure password protection policy. Springsecurity uses bcrypt encryption algorithm by default. / / you can return new BCryptPasswordEncoder ();}} 3 as long as you explicitly declare BCryptPasswordEncoder Bean here. Implement UserDetailsService

Create the service package and create the SimpleUserDetailsService class to implement the UserDetailsService interface.

The complete code is as follows:

Package net.txt100.learn.springsecurity.base.case2.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.security.core.authority.SimpleGrantedAuthority;import org.springframework.security.core.userdetails.User;import org.springframework.security.core.userdetails.UserDetails;import org.springframework.security.core.userdetails.UserDetailsService;import org.springframework.security.core.userdetails.UsernameNotFoundException;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.crypto.password.PasswordEncoder;import org.springframework.stereotype.Service Import java.util.Collections;/** * Title: SimpleUserDetailsService * Package: net.txt100.learn.springsecurity.base.case2.service * Creation date: 2019-08-11 * Description: use predefined user information * * @ author Tonglei * @ since 1.0 * / @ Servicepublic class SimpleUserDetailsService implements UserDetailsService {@ Autowired private PasswordEncoder passwordEncoder @ Override public UserDetails loadUserByUsername (String username) throws UsernameNotFoundException {/ / returns the user name, password and permission information of the user based on the user name / / any user name + password 123456 in this example can be logged in successfully / / ROLE_* is a spring security-style role definition Here * you can replace User user = new User (username, passwordEncoder.encode ("123456"), Collections.singleton (new SimpleGrantedAuthority ("ROLE_USER") Return user;}} 4. Compilation and execution

Open the command line, enter the project root directory, and execute the compilation command

Gradle compileJava

Execute the run command

Gradle run

Visit http://localhost:8080/user/all

The above is the editor for you to share how to customize user information in Spring security, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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