1 第一步
搭建一个基于maven的web工程
2 第二步
在web.xml配置文件中写上如下代码
Archetype Created Web Application
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-security.xml
1
springmvc
*.do
第三步:建立Spring-security的配置文件
4 关于第三步配置文件的讲解
4-1 配置拦截
- auto-config 自动配置,如果设置为true,表示自动应用一些默认配置,比如框架会提供一个默认的登录页面,会帮我们提供一个Spring生成的Controller
- use-expressions:是否使用spring security提供的表达式来描述权限
- pattern:描述拦截规则
- asscess:指定所需的访问角色或者访问权限
4-2 配置认证管理器
这段代码表示写死一个用户,其中,{noop}表示密码使用明文来登录,不加密。
4-3 配置可匿名访问,不用登录就可以访问
4-4 使用指定的登录页面
第一步对登录页面放行,不拦截
第二步:定义表单登录信息
第3步,关闭csrf过滤器
5 从数据库查询用户信息
5-1
编写一个类SpringSecurityUserService,实现UserDetailsService接口并重写方法,框架会自动帮我们调用这个方法,username就是前端登录的用户名,但是框架怎样就能自动帮我们调用一个我们自己写的方法呢,所以,我们需要配置一下
public class SpringSecurityUserService implements UserDetailsService {
@java.lang.Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//这个方法框架帮我们调用,username是登录传进来的,但我们要配置这个类。
System.out.println("输入的用户名是"+username);
return null;
}
}
5-2 在配置文件中配置刚才5-1写的类
2个地方,第一个,需要配置
,
第二个,需要引用这个对象。
5-3 模拟从数据库查询
package com.itheima.service;
import com.itheima.pojo.User;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SpringSecurityUserService implements UserDetailsService {
//模拟数据库中的用户数据
public static Map map = new HashMap<>();
static {
com.itheima.pojo.User user1 = new com.itheima.pojo.User();
user1.setUsername("admin");
user1.setPassword("admin");//明文密码(没有加密)
com.itheima.pojo.User user2 = new com.itheima.pojo.User();
user2.setUsername("xiaoming");
user2.setPassword("1234");
map.put(user1.getUsername(),user1);
map.put(user2.getUsername(),user2);
}
//根据用户名查询用户信息
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
System.out.println("用户输入的用户名为:" + username);
//根据用户名查询数据库获得用户信息(包含数据库中存储的密码信息)
User user = map.get(username);//模拟查询根据用户名查询数据库
if(user == null){
//用户名不存在
return null;
}else{
//将用户信息返回给框架
//框架会进行密码比对(页面提交的密码和数据库中查询的密码进行比对)
List list = new ArrayList<>();
//为当前登录用户授权,后期需要改为从数据库查询当前用户对应的权限
list.add(new SimpleGrantedAuthority("permission_A"));//授权
list.add(new SimpleGrantedAuthority("permission_B"));
if(username.equals("admin")){
list.add(new SimpleGrantedAuthority("ROLE_ADMIN"));//授予角色
}
org.springframework.security.core.userdetails.User securityUser = new org.springframework.security.core.userdetails.User(username,"{noop}"+user.getPassword(),list);
return securityUser;
}
}
}
5-4 不同权限控制
- isAuthenticated()只要认证通过就可以放访问
- hasAuthority()只要拥有某个角色就可以通过
- hasRole()有某个角色才可以通过
5-5 通过注解控制权限
第一步:
在Spring-security.xml中配置组建扫描,扫描controller
第二步
开启注解方式权限控制
第三步
创建controller类并使用权限注解
package com.itheima.controller;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/add")
@PreAuthorize("hasAuthority('add')")//表示用户必须拥有add权限才能调用当前方法
public String add(){
System.out.println("add...");
return "success";
}
@RequestMapping("/delete")
@PreAuthorize("hasRole('ROLE_ADMIN')")//表示用户必须拥有ROLE_ADMIN角色才能调用当前方法
public String delete(){
System.out.println("delete...");
return "success";
}
}
6 退出登录
7 完成版的Spring-security.xml内容