在web开发中,安全第一位。 过滤器,拦截器
spring security 是针对 Spring项目的安全框架,也是Spring Boot 底层安全模块默认的技术选型,它可以实现强大的Web安全控制, 对于安全控制,我们仅需要引入spring-boot-starter-security 模块,进行少量的配置,即可实现强大的安全管理!
记住几个类:
- WebSecurityConfigurerAdapter: 自定义Security策略
- AuthenticationManagerBuilder:自定义认证策略
- @EnableWebSecurity:开启WebSecurity模式
Spring Security 的两个主要目标是”认证“和”授权“(访问控制)。
”认证“(Authentication)
”授权“(Authorization)
这个概念是通用的,而不是只在Spring security 中存在。
如果有一个权限模块的话
public class RouterController {
@RequestMapping({"/","index"})
public String index(){
return "index";
}
@RequestMapping("/toLogin")
public String toLogin(){
return "views/login";
}
//可以用Restful风格来设置,按等级分类
@RequestMapping("/level1/{id}")
public String level1(@PathVariable("id")int id){
return "views/level1/"+id;
}
@RequestMapping("/level2/{id}")
public String level2(@PathVariable("id")int id){
return "views/level2/"+id;
}
@RequestMapping("/level3/{id}")
public String level3(@PathVariable("id")int id){
return "views/level3/"+id;
}
}
package com.nmsl.config;
import org.springframework.boot.autoconfigure.security.servlet.WebSecurityEnablerConfiguration;
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.WebSecurityConfiguration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
//AOP:拦截器!
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//授权
@Override
protected void configure(HttpSecurity http) throws Exception { //Http安全策略
//首页所有人可以访问,但是功能页只有对应有权限的人才能访问
http.authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/level1/**").hasRole("vip1")
.antMatchers("/level2/**").hasRole("vip2")
.antMatchers("/level3/**").hasRole("vip3");
//没有权限默认到登陆页面,需要开启登录的页面
// http.formLogin();
//定制登录页
//http.formLogin().loginPage("/toLogin");
//http.formLogin().loginPage("/toLogin");
http.formLogin().loginPage("/toLogin").usernameParameter("username").passwordParameter("pwd").loginProcessingUrl("/login");
//防止网站攻击,关闭跨站脚本请求攻击,登录失败可能存在的原因
http.csrf().disable();
//开启记住我功能
http.rememberMe().rememberMeParameter("remeber");
//开启了注销功能
http.logout().logoutSuccessUrl("/");
}
//认证,
//如果没有加入passwordEncode(),会报错误 There is no PasswordEncoder mapped for the id "null" 500错误,没有设置密码编码格式
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//正常应该从数据库中读取
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser("admin").password(new BCryptPasswordEncoder().encode("1212")).roles("vip1","vip2","vip3")
.and().withUser("nmsl").password(new BCryptPasswordEncoder().encode("1212")).roles("vip2")
.and().withUser("guest").password(new BCryptPasswordEncoder().encode("1212")).roles("vip1");
}
}
前端
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<!--登录注销-->
<div class="right menu">
<!--如果未登录-->
<div sec:authorize="!isAuthenticated()" >
<a class="item" th:href="@{/toLogin}">
<i class="address card icon"></i> 登录
</a>
</div>
<!--如果登录:用户名,注销按钮-->
<div sec:authorize="isAuthenticated()" >
<a class="item" >
用户名:<span sec:authentication="name"></span>
角色:<span sec:authentication="principal.authorities"></span>
</a>
</div>
<div sec:authorize="isAuthenticated()" >
<a class="item" th:href="@{/logout}">
<i class="address card icon"></i> 注销
</a>
</div>
<!--菜单根据用户的角色动态的实现,sec:authorize="hasRole('')"-->
<div class="column" sec:authorize="hasRole('vip1')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 1</h5>
<hr>
<div><a th:href="@{/level1/1}"><i class="bullhorn icon"></i> Level-1-1</a></div>
<div><a th:href="@{/level1/2}"><i class="bullhorn icon"></i> Level-1-2</a></div>
<div><a th:href="@{/level1/3}"><i class="bullhorn icon"></i> Level-1-3</a></div>
</div>
</div>
</div>
</div>
<div class="column" sec:authorize="hasRole('vip2')">
<div class="ui raised segment" >
<div class="ui">
<div class="content">
<h5 class="content">Level 2</h5>
<hr>
<div><a th:href="@{/level2/1}"><i class="bullhorn icon"></i> Level-2-1</a></div>
<div><a th:href="@{/level2/2}"><i class="bullhorn icon"></i> Level-2-2</a></div>
<div><a th:href="@{/level2/3}"><i class="bullhorn icon"></i> Level-2-3</a></div>
</div>
</div>
</div>
</div>
<div class="column" sec:authorize="hasRole('vip3')">
<div class="ui raised segment">
<div class="ui">
<div class="content">
<h5 class="content">Level 3</h5>
<hr>
<div><a th:href="@{/level3/1}"><i class="bullhorn icon"></i> Level-3-1</a></div>
<div><a th:href="@{/level3/2}"><i class="bullhorn icon"></i> Level-3-2</a></div>
<div><a th:href="@{/level3/3}"><i class="bullhorn icon"></i> Level-3-3</a></div>
</div>
</div>
</div>
</div>