SpringBoot--配置SpringSecurity用户库安全认证

1、安全认证原理图

SpringBoot--配置SpringSecurity用户库安全认证_第1张图片

注解:而我们要做的,就是实现这个UserDetailService,

它就是连接我们的数据库和Acegi的桥梁。UserDetailService的要求也很简单,只需要一个返回org.springframework.security.userdetails.User对象的loadUserByUsername(String userName)方法。

2、POM.xml



	4.0.0
	
		com.zemel
		microcloud
		1.0.0
	
	microcloud-user
	microcloud-user
	http://maven.apache.org
	
		UTF-8
	
	
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		
		
			org.thymeleaf.extras
			thymeleaf-extras-springsecurity4
		
		
			mysql
			mysql-connector-java
		
		
			org.springframework.boot
			spring-boot-starter-security
		
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		
		
            org.springframework.data
            spring-data-jpa
        
	

3、CustomUserService.java

package com.zemel.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.zemel.dao.SysUserRepository;
import com.zemel.entity.SysUser;

public class CustomUserService implements UserDetailsService {

	@Autowired
	SysUserRepository userRepository;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

		SysUser user = userRepository.findByUsername(username);
		if (user == null) {
			throw new UsernameNotFoundException("用户名不存在");
		}
		System.out.println("username:" + username);
		System.out.println("username:" + user.getUsername() + ";password:" + user.getPassword());
		return user;
	}

}

4、WebSecurityConfig.java

package com.zemel.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

import com.zemel.service.CustomUserService;

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

	@Bean
	UserDetailsService customUserService() {
		return new CustomUserService();
	}

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(customUserService());
	}
	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		 http.authorizeRequests()
		 	.anyRequest().authenticated() // 任何请求都需要验证 
		 	.and().formLogin().loginPage("/login").failureUrl("/login?error").permitAll().and() // 登录页面无权限访问
		 	.logout().permitAll(); // 注销无权限访问
	}
	
	
}

5、工程代码,下载地址

https://download.csdn.net/download/zhangminemail/10829246

参考:https://blog.csdn.net/u012702547/article/details/54319508

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(SpringBoot)