再web开发中,安全第一位! 过滤器,拦截器~
功能性需求:否
做网站:安全应该再什么时候考虑? 设计之初
* 漏洞,隐私泄露等等
* 架构一旦确定!
shiro,springSevurity:很像,除了类不一样,名字不一样;
认证,授权(vip1,vip2,vip3)
- 功能权限
- 访问权限
- 菜单权限
- 拦截器,过滤器:大量的原生代码~
环境(springsecurity)
package com.zsy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class RouterController {
/**
* 首页
* @return index 页面
*/
@RequestMapping({"/","/index"})
public String index(){
return "index";
}
/**
* 登陆页面
* @return login页面
*/
@RequestMapping("/toLogin")
public String toLogin(){
return "view/logjin";
}
/**
* 赋予权限,哪个级别能看哪个页面
* @param id
* @return
*/
@RequestMapping("t/leve1/{id}")
public String level1(@PathVariable("id") int id){
return "view/level1/+id";
}
@RequestMapping("t/leve2/{id}")
public String level2(@PathVariable("id") int id){
return "view/level2/+id";
}
@RequestMapping("t/leve3/{id}")
public String level3(@PathVariable("id") int id){
return "view/level3/+id";
}
}
springSecurity
##简介
SpringSecurity是针对Spring项目的安全矿价格,也是Spring Boot底层安全模块默认的技术选型,它可以实现强大的web安全控制,对于安全控制,我们仅需要引入spring-boot-security模块,进行少量配置,即可实现强大的安全管理!
记住几个类:
- WebSecurityConfigurerAdapter: 自定义Security策略
- AuthenticationManagerBuilder: 自定义认证策略
- @EnableWebSecurity:开启WebSecurity模式 @Enablexxxx,开启某个功能
SpringSecurity的俩个主要目标是“认证”和“授权”(访问控制)
“认证”(Authentication)
“授权”(Authorization)
这个概念是通用的,而不是只再Spring Security中存在
package com.zsy.config;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@EnableWebSecurity
public class SecurityConfing extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception {
// 首页所有人可以访问,功能页只有对应有权限的人才能访问
//请求授权的规则(403)
http.authorizeRequests().antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限默认到登陆页面,需要开启登陆的页面
//为什么进入Loginl里面
//定制登录页
http.formLogin().loginPage("/toLogin")
//默认username自定义user
.usernameParameter("user")
//默认password自定义pwd
.passwordParameter("pwd")
//自定义授权路径
.loginProcessingUrl("login");
//注销,开启了注销功能 并跳转到首页
//防止网站攻击: get不安全,post
http.csrf().disable();//关闭csrf功能,登陆失败可能存在的原因。
http.logout().logoutSuccessUrl("/");
//开启记住我功能 cookie,默认保存2周,,自定义接收前端参数
http.rememberMe().rememberMeParameter("remember");
}
//认证,从springboot 2.1.x可以直接使用
//密码编码:passwordEncoder
//再sprign Security 5.0+新增很多的加密方法~
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//这些数据正常应该从数据库中读
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("唐山").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2")
.and()
.withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1","vip2","vip3");
}
}