学习security(一)
一.security是什么?
Spring Security 是基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案。一般来说,Web 应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分。用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。用户授权指的是验证某个用户是否有权限执行某个操作。 在一个系统中,不同用户所具有的权限是不同的。比如对一个文件来说,有的用户只能进行读取,而有的用户可以进行修改。一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限。
总的来说:security简单说就是很多过滤器一起使用
**
spring security实现方式大致可以分为这几种:
1.配置文件实现,只需要在配置文件中指定拦截的url所需要权限、配置userDetailsService指定用户名、密码、对应权限,就可以实现。
2.实现UserDetailsService,loadUserByUsername(String userName)方法,根据userName来实现自己的业务逻辑返回UserDetails的实现类,需要自定义User类实现UserDetails,比较重要的方法是getAuthorities(),用来返回该用户所拥有的权限。
3.通过自定义filter重写spring security拦截器,实现动态过滤用户权限。
4.通过自定义filter重写spring security拦截器,实现自定义参数来检验用户,并且过滤权限。
二.在springboot中基于内存的security使用(账号密码后台定义)
没有登录进入首页后,访问内部网页会直接跳转到登录界面;登录成功后进入内部界面,注销后回到首页。如下:
1.首先创建springboot项目
2.引入相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--security安全认证-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<!--视图解析-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3.创建security配置类SecurityConfig,继承WebSecurityConfigurerAdapter父类
重写configure方法:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin().loginPage("/login")
.failureUrl("/login-error")//登录失败页面
.permitAll() //表单登录,permitAll()表示这个不需要验证 登录页面,登录失败页面
.and()
.authorizeRequests()
.antMatchers("/").permitAll() //这就表示 /index这个页面不需要权限认证,所有人都可以访问
.anyRequest().authenticated()
.and()
.csrf().disable().logout().logoutSuccessUrl("/")
.permitAll(); // 允许登出;
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// 存中创建了一个用户,该用户的名称为user,密码为123456,用户角色为USER
// 对用户密码进行了加密
auth.inMemoryAuthentication()
.passwordEncoder(new BCryptPasswordEncoder())
.withUser("user").password(new BCryptPasswordEncoder().encode("123456")).roles("USER");
}
}
4.新建UserController类,添加跳转映射
@Controller
public class UserController {
/*
跳转登录
*/
@RequestMapping("login")
public String login() {
return "login";
}
/*
内部页面
*/
@GetMapping("hello")
public String sayHello2() {
return "hello";
}
@GetMapping("/")
public String index() {
return "index";
}
/*
错误页面
*/
@GetMapping("/login-error")
public String error() {
return "login-error";
}
}
5.首页index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主页</title>
</head>
<body>
<h4>这是主页</h4>
去 <a href="/hello">内部页面</a>
</body>
</html>
6.内部页面hello.index
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>内部界面</title>
</head>
<body>
<h4>欢迎来到securityDemo</h4>
<form action="/logout">
<button type="submit">注销</button>
</form>
</body>
</html>
7.登录login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h3>登录</h3>
<hr>
<form action="/login" method="post">
<label for="username">昵 称:</label>
<input type="text" id="username" name="username"><br>
<label for="password">密 码:</label>
<input type="password" id="password" name="password"><br>
<button type="submit">提交</button>
</form>
</body>
</html>
8.错误login-error.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>错误</title>
</head>
<body>
<h3>登录错误</h3>
请重新 <a href="/login">登录</a>
</body>
</html>
9.运行项目