Security的基本介绍搜搜别的博客看吧,我是来贴代码的(如果是新手,一定要多看看基本介绍,搭建,配置等)
最重要的Security配置代码
package com.**.config;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import com.**.core.service.CustomerUserService;
@Configuration
@EnableWebSecurity //打开SpringSecurity的web支持
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
UserDetailsService customerUserService(){
return new CustomerUserService();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//在内存中创建一个ADMIN角色的用户
// auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder()) //5.0以上的security版本需要一个密码编码器 需要自定义
// .withUser("testoc").password("0bf1e4984cd85c0eb227856324b72901").roles("supperManager");
//此处可以创建多个用户
// auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder())
// .withUser("demo").password("demo").roles("USER");
//使用数据库管理用户
//customerUserService()是获取数据库动态用户和角色数据 new MyPasswordEncoder()提供密码的MD5加密对比
}
package com.**.config;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import com.**.RunMain;
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RunMain.class);
}
}
提供密码对比的代码
package com.**.config;
import org.springframework.security.crypto.password.PasswordEncoder;
import com.**.core.service.CustomerUserService;
import com.**.core.util.MD5Util;
public class MyPasswordEncoder implements PasswordEncoder {
public String encode(CharSequence rawPassword) {
//加密方法
return rawPassword.toString();
}
public boolean matches(CharSequence rawPassword, String encodedPassword) {
try {//个人方法将密码和用户id加密 大家可以自定义
Integer id = CustomerUserService.getUser().getId();
if(MD5Util.checkPasswordWithSalt(rawPassword.toString(),encodedPassword, id)) {//参数(页面明文密码,数据库加密密码,用户id)
return true;
}
return false;
}catch(Exception e){
return false;
}
}
public static void main(String[] args) {
System.out.println(MD5Util.getMD5EncodedPasswordWithSalt("0bf1e4984cd85c0eb227856324b72901",33));
}
}
动态获取用户数据代码
package com.**.core.service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import com.**.core.entity.Role;
import com.**.core.entity.User;
import com.**.core.mapper.RoleMapper;
import com.**.core.mapper.UserMapper;
//继承UserDetailsService 会自动找到loadUserByUsername方法获取动态数据
public class CustomerUserService implements UserDetailsService {
@Autowired
private UserMapper userMapper;
@Autowired
private RoleMapper roleMapper;
static User user;
public static User getUser() {
return user;
}
public static void setUser(User user) {
CustomerUserService.user = user;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User sysUser = userMapper.loadUserByUsername(username);
if(sysUser == null){
throw new UsernameNotFoundException("用户名不存在");
}
user = sysUser;
List
Role role = roleMapper.selectByPrimaryKey(new BigDecimal(sysUser.getRoleid()));
sysUser.setRole(role);//重点"ROLE_"+role.getCode() ROLE_必须加 判断权限会以ROLE_开头判断 看看源码就可以理解
authorities.add(new SimpleGrantedAuthority("ROLE_"+role.getCode()));
return new org.springframework.security.core.userdetails.User(sysUser.getLoginName(),sysUser.getPassword(),authorities);
}
}
接下来贴前端页面代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page isELIgnored ="false" %>
<%@ include file="/page/common/pub.jsp" %>
退出登录
登录是自定义页面,所以转页面是用的get方法
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginPage() {
return "login/login";
}
提交登录是post
退出登录是security的方法,用form表单post提交