博客
关于我
Spring security安全框架的使用
阅读量:598 次
发布时间:2019-03-12

本文共 6340 字,大约阅读时间需要 21 分钟。

入门案例

创建一个maven工程

第一步导入pom.xml

4.0.0
com.offcn
spring_security_demo
1.0-SNAPSHOT
war
3.0
4.2.5.RELEASE
org.springframework
spring-core
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework.security
spring-security-web
4.1.0.RELEASE
org.springframework.security
spring-security-config
4.1.0.RELEASE
javax.servlet
servlet-api
2.5
provided
maven-compiler-plugin
1.8
1.8
org.apache.tomcat.maven
tomcat7-maven-plugin
9090
/

框架所必须的依赖

org.springframework.security
spring-security-web
4.1.0.RELEASE
org.springframework.security
spring-security-config
4.1.0.RELEASE

第二步配置web.xml

contextConfigLocation
classpath:spring/application_security.xml
org.springframework.web.context.ContextLoaderListener
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*

第三步写application_security.xml配置文件

第四步写页面

login.html

注意:1.用户名name必须是username 2.密码name必须是possword 3.提交地址必须是/login 4.提交方式必须是post

用户名:
密码

成功页面还要失败页面就不写了

将Spring security融入项目中的操作

第一步在web.xml中添加一个过滤器

springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*

第二步写application_security.xml配置文件

第三步 修改登陆表单的action还要属性名等

注意:登陆访问的接口是/login 注销操作访问的接口是/logout

注销举例:

注销
内容补充:当你登陆成功后还要获取登陆名信息的话,再写一个接口
@RestController@RequestMapping("/login")public class LongController {       @RequestMapping("/name")    public Map name(){           //Context上下文        String name = SecurityContextHolder.getContext().getAuthentication().getName();        Map map = new HashMap();        map.put("loginName",name);        return map;    }}

自定义认证类

1.写一个类实现UserDetailsService接口

package com.offcn.service;import org.springframework.security.core.GrantedAuthority;import org.springframework.security.core.authority.SimpleGrantedAuthority;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 java.util.ArrayList;import java.util.List;public class UserDetailsServiceImpl implements UserDetailsService {       //UserDetails 认证的一个接口 有一个实现类 User    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {          //自定义一个安全的集合  ROLE_TEACGER 是配置拥有访问权限的用户的角色    GrantedAuthority:权限        List
Granted = new ArrayList
(); //角色名必须是配置文件中已经配置的角色 Granted.add(new SimpleGrantedAuthority("ROLE_SELLER")); return new User(username,"123",Granted); //这里写username 表示用户名可以任意 密码是123 就可以通过验证 }}

2.修改spring-security.xml配置文件

只修改认证管理器

转载地址:http://tobxz.baihongyu.com/

你可能感兴趣的文章
MySQL-Explain的详解
查看>>
mysql-group_concat
查看>>
MySQL-redo日志
查看>>
MySQL-【1】配置
查看>>
MySQL-【4】基本操作
查看>>
Mysql-丢失更新
查看>>
Mysql-事务阻塞
查看>>
Mysql-存储引擎
查看>>
mysql-开启慢查询&所有操作记录日志
查看>>
MySQL-数据目录
查看>>
MySQL-数据页的结构
查看>>
MySQL-架构篇
查看>>
MySQL-索引的分类(聚簇索引、二级索引、联合索引)
查看>>
Mysql-触发器及创建触发器失败原因
查看>>
MySQL-连接
查看>>
mysql-递归查询(二)
查看>>
MySQL5.1安装
查看>>
mysql5.5和5.6版本间的坑
查看>>
mysql5.5最简安装教程
查看>>
mysql5.6 TIME,DATETIME,TIMESTAMP
查看>>