Getting started with springboot + spring security

Zhe Qi Nei Rong Dang Zhong Xiao Bian Jiang Hui Gei Da Jia Dai Lai You Guan Jian Dan Ru Men SpringBoot+Spring Security, Wen Zhang Nei Rong Feng Fu Qie Yi Zhuan Ye De Jiao Du Wei Da Jia Fen Xi He Xu Shu , Yue Du Wan Zhe Pian Wen Zhang Xi Wang Da Jia Ke Yi You Suo Shou Huo 。

Yi 、Spring Security Ji Ben Jie Shao

Zhe Li Jiu Bu Dui Spring Security Jin Xing Guo Duo De Jie Shao Liao , Ju Ti De Ke Yi Can Kao Guan Fang Wen Dang

Wo Jiu Zhi Shuo Xia SpringSecurity He Xin Gong Neng :

  1. Ren Zheng ( Ni Shi Shui )

  2. Shou Quan ( Ni Neng Gan Shi Me )

  3. Gong Ji Fang Hu ( Fang Zhi Wei Zao Shen Fen )

Er 、 Ji Ben Huan Jing Da Jian

Zhe Li Wo Men Yi SpringBoot Zuo Wei Xiang Mu De Ji Ben Kuang Jia , Wo Zhe Li Shi Yong De Shi maven De Fang Shi Lai Jin Xing De Bao Guan Li , Suo Yi Zhe Li Xian Gei Chu Ji Cheng Spring  Security De Fang Shi

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">     <parent>         <artifactId>jeecg-boot-cloud-study</artifactId>         <groupId>com.jeecg.cloud</groupId>         <version>1.0.0</version>     </parent>     <modelVersion>4.0.0</modelVersion>      <artifactId>jeecg-boot-security</artifactId>      <dependencies>         <dependency>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-starter-web</artifactId>         </dependency>             <dependency>                 <groupId>org.springframework.boot</groupId>                 <artifactId>spring-boot-starter-security</artifactId>             </dependency>     </dependencies>  </project>

Ran Hou Jian Li Yi Ge Web Ceng Qing Qiu Jie Kou

@RestController @RequestMapping("/user") public class UserController {   @GetMapping   public String getUsers() {         return "Hello Jeecg Spring Security";   } }

Jie Xia Lai Ke Yi Zhi Jie Jin Xing Xiang Mu De Yun Xing , Bing Jin Xing Jie Kou De Diao Yong Kan Kan Xiao Guo Liao 。

San 、 Tong Guo Wang Ye De Diao Yong

Wo Men Shou Xian Tong Guo Zuo Lan Qi Jin Xing Jie Kou De Diao Yong , Zhi Jie Fang Wen http://localhost:8080/user, Ru Guo Jie Kou Neng Zheng Chang Fang Wen , Na Me Ying Gai Xian Shi “Hello Jeecg  Spring Security”。

Dan Shi Wo Men Shi Mei Fa Zheng Chang Fang Wen De , Chu Xian Liao Xia Tu De Shen Fen Yan Zheng Shu Ru Kuang

 Jian  Dan  Ru  Men SpringBoot+Spring Security

Zhe Shi Yin Wei Zai SpringBoot Zhong , Yin Ru De Spring Security Yi Lai , Quan Xian Kong Zhi Zi Dong Sheng Xiao Liao , Ci Shi De Jie Kou Du Shi Bei Bao Hu De , Wo Men Xu Yao Tong Guo Yan Zheng Cai Neng Zheng Chang De Fang Wen 。  Spring Security Ti Gong Liao Yi Ge Mo Ren De Yong Hu , Yong Hu Ming Shi user, Er Mi Ma Ze Shi Qi Dong Xiang Mu De Shi Hou Zi Dong Sheng Cheng De 。

Wo Men Cha Kan Xiang Mu Qi Dong De Ri Zhi , Hui Fa Xian Ru Xia De Yi Duan Log

  • Using default security password: 62ccf9ca-9fbe-4993-8566-8468cc33c28c

Dang Ran Ni Kan Dao De password Ken Ding He Wo Shi Bu Yi Yang De , Wo Men Zhi Jie Yong user He Qi Dong Ri Zhi Zhong De Mi Ma Jin Xing Deng Lu 。

Deng Lu Cheng Gong Hou , Jiu Tiao Zhuan Dao Liao Jie Kou Zheng Chang Diao Yong De Ye Mian Liao 。

Ru Guo Bu Xiang Yi Kai Shi Jiu Shi Neng Spring Security, Ke Yi Zai Pei Zhi Wen Jian Zhong Zuo Ru Xia De Pei Zhi :

# security  Shi  Neng  security.basic.enabled = false

Gang Cai Kan Dao De Deng Lu Kuang Shi SpringSecurity Shi Kuang Jia Zi Ji Ti Gong De , Bei Cheng Wei httpBasicLogin。 Xian Shi Ta Bu Shi Wo Men Chan Pin Shang Xiang Yao De , Wo Men Qian Duan Yi Ban Shi Tong Guo Biao Dan Ti Jiao De Fang Shi Jin Xing Yong Hu Deng Lu Yan Zheng De , Suo Yi Wo Men Jiu Xu Yao Zi Ding Yi Zi Ji De Ren Zheng Luo Ji Liao 。

Si 、 Zi Ding Yi Yong Hu Ren Zheng Luo Ji

Mei Ge Xi Tong Ken Ding Shi You Zi Ji De Yi Tao Yong Hu Ti Xi De , Suo Yi Wo Men Xu Yao Zi Ding Yi Zi Ji De Ren Zheng Luo Ji Yi Ji Deng Lu Jie Mian 。 Zhe Li Wo Men Xu Yao Xian Dui SpringSecurity Jin Xing Xiang Ying De Pei Zhi

package org.jeecg.auth.config;  import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;  @Configuration public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {      @Override     protected void configure(HttpSecurity http) throws Exception {         http.formLogin()          //  Ding  Yi  Dang  Xu  Yao  Yong  Hu  Deng  Lu  Shi  Hou , Zhuan  Dao  De  Deng  Lu  Ye  Mian 。                 .loginProcessingUrl("/user/login") //  Zi  Ding  Yi  De  Deng  Lu  Jie  Kou                  .and()                 .authorizeRequests()    //  Ding  Yi  Na  Xie URL Xu  Yao  Bei  Bao  Hu 、 Na  Xie  Bu  Xu  Yao  Bei  Bao  Hu                  .anyRequest()        //  Ren  He  Qing  Qiu , Deng  Lu  Hou  Ke  Yi  Fang  Wen                  .authenticated();     } }

Zi Ding Yi Mi Ma Jia Mi Jie Mi

package org.jeecg.auth.config;  import org.springframework.context.annotation.Bean; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Component;  @Component public class MyPasswordEncoder implements PasswordEncoder {     @Override     public String encode(CharSequence charSequence) {         return charSequence.toString();     }      @Override     public boolean matches(CharSequence charSequence, String s) {         return s.equals(charSequence.toString());     } }

Jie Xia Lai Zai Pei Zhi Yong Hu Ren Zheng Luo Ji , Yin Wei Wo Men Shi You Zi Ji De Yi Tao Yong Hu Ti Xi De

package org.jeecg.auth.config;  import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.authority.AuthorityUtils; 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.factory.PasswordEncoderFactories; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Component;  @Component public class MyUserDetailsService implements UserDetailsService {     private Logger logger = LoggerFactory.getLogger(getClass());      @Autowired     private PasswordEncoder passwordEncoder;      @Override     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {         logger.info(" Yong  Hu  De  Yong  Hu  Ming : {}", username);         // TODO  Gen  Ju  Yong  Hu  Ming , Cha  Zhao  Dao  Dui  Ying  De  Mi  Ma , Yu  Quan  Xian           //  Feng  Zhuang  Yong  Hu  Xin  Xi , Bing  Fan  Hui 。 Can  Shu  Fen  Bie  Shi : Yong  Hu  Ming , Mi  Ma , Yong  Hu  Quan  Xian          User user = new User(username, passwordEncoder.encode("123456"), AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));         return user;     } }

Zhe Li Wo Men Mei You Jin Xing Guo Duo De Xiao Yan , Yong Hu Ming Ke Yi Sui Yi De Tian Xie , Dan Shi Mi Ma Bi Xu De Shi “123456”, Zhe Yang Cai Neng Deng Lu Cheng Gong 。

Tong Shi Ke Yi Kan Dao , Zhe Li User Dui Xiang De Di San Ge Can Shu , Ta Biao Shi De Shi Dang Qian Yong Hu De Quan Xian , Wo Men Jiang Ta She Zhi Wei ”admin”。

Wo Men Zhe Li Sui Bian Tian Xie Yi Ge user, Ran Hou Password Xie Tian Xie Yi Ge Cuo Wu De ( Fei 123456) De 。 Zhe Shi Hui Ti Shi Xiao Yan Cuo Wu :

 Jian  Dan  Ru  Men SpringBoot+Spring Security

Tong Shi Zai Kong Zhi Tai , Ye Hui Da Yin Chu Gang Cai Deng Lu Shi Tian Xie De user

Xian Zai Wo Men Zai Lai Shi Yong Zheng Que De Mi Ma Jin Xing Deng Lu Shi Shi , Ke Yi Fa Xian Jiu Hui Tong Guo Xiao Yan , Tiao Zhuan Dao Zheng Que De Jie Kou Diao Yong Ye Mian Liao 。

Liu 、UserDetails

Gang Gang Wo Men Zai Xie MyUserDetailsService De Shi Hou , Li Mian Shi Xian Liao Yi Ge Fang Fa , Bing Fan Hui Liao Yi Ge UserDetails。 Zhe Ge UserDetails   Jiu Shi Feng Zhuang Liao Yong Hu Xin Xi De Dui Xiang , Li Mian Bao Han Liao Qi Ge Fang Fa

public interface UserDetails extends Serializable {   //  Feng  Zhuang  Liao  Quan  Xian  Xin  Xi    Collection<? extends GrantedAuthority> getAuthorities();   //  Mi  Ma  Xin  Xi    String getPassword();   //  Deng  Lu  Yong  Hu  Ming    String getUsername();   //  Zhang  Hu  Shi  Fou  Guo  Qi    boolean isAccountNonExpired();   //  Zhang  Hu  Shi  Fou  Bei  Dong  Jie    boolean isAccountNonLocked();   //  Zhang  Hu  Mi  Ma  Shi  Fou  Guo  Qi , Yi  Ban  You  De  Mi  Ma  Yao  Qiu  Xing  Gao  De  Xi  Tong  Hui  Shi  Yong  Dao , Bi  Jiao  Mei  Ge  Yi  Duan  Shi  Jian  Jiu  Yao  Qiu  Yong  Hu  Zhong  Zhi  Mi  Ma    boolean isCredentialsNonExpired();   //  Zhang  Hao  Shi  Fou  Ke  Yong    boolean isEnabled(); }

Wo Men Zai Fan Hui UserDetails De Shi Xian Lei User De Shi Hou , Ke Yi Tong Guo User De Gou Zao Fang Fa , She Zhi Dui Ying De Can Shu

Qi 、 Mi Ma Jia Mi Jie Mi

SpringSecurity Zhong You Yi Ge PasswordEncoder Jie Kou

public interface PasswordEncoder {   //  Dui  Mi  Ma  Jin  Xing  Jia  Mi    String encode(CharSequence var1);   //  Dui  Mi  Ma  Jin  Xing  Pan  Duan  Pi  Pei    boolean matches(CharSequence var1, String var2); }

Wo Men Zhi Xu Yao Zi Ji Shi Xian Zhe Ge Jie Kou , Bing Zai Pei Zhi Wen Jian Zhong Pei Zhi Yi Xia Jiu Ke Yi Liao 。

Shang Shu Jiu Shi Xiao Bian Wei Da Jia Fen Xiang De Jian Dan Ru Men SpringBoot+Spring Security Liao , Ru Guo Gang Hao You Lei Si De Yi Huo , Bu Fang Can Zhao Shang Shu Fen Xi Jin Xing Li Jie 。 Ru Guo Xiang Zhi Dao Geng Duo Xiang Guan Zhi Shi , Huan Ying Guan Zhu Tumi Yun Xing Ye Zi Xun Pin Dao 。

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment