Spring Boot 集成 Spring Security 使用
创建时间:2018-05-19  访问量:8958  6  2

Spring Boot 集成 Spring Security 使用

UserDetailsBean.java后续用用到。 

package com.zxstrive.fight.sys.extend.security;

import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.zxstrive.fight.app.entity.jpa.UserEntity;

public class UserDetailsBean implements UserDetails {

	private static final long serialVersionUID = 1L;
	
    //系统定义的User表
	private UserEntity user = new UserEntity();
	
    //角色列表
	private List<GrantedAuthority> authorities;
	
	public UserDetailsBean() {}
	
	public UserDetailsBean(String loginName, String password) {
		this.user.setLoginName(loginName);
		this.user.setPassword(password);
	}
	

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		return authorities;
	}

	@Override
	public String getPassword() {
		return user.getPassword();
	}

	@Override
	public String getUsername() {
		return user.getLoginName();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}
	
	public void setUsername(String username) {
		this.user.setLoginName(username);
	}
	
	public void setPassword(String password) {
		this.user.setPassword(password);
	}
	
	public void setAuthorities(List<GrantedAuthority> authorities) {
		this.authorities = authorities;
	}
	
	@Override
	public boolean equals(Object obj) {
		UserDetailsBean u = (UserDetailsBean)obj;
		return this.toString().equals(u.toString());
	}
	
	@Override
	public int hashCode() {
		return this.user.getLoginName().hashCode();
	}
	
	@Override
	public String toString() {
		return this.user.getLoginName();
	}

	public UserEntity getUser() {
		return user;
	}

	public void setUser(UserEntity user) {
		this.user = user;
	}	
	
}

在部分内容根据接口继承过来理解一下即可。