一、Spring security ?
Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架
它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了Spring IoC,DI(控制反转Inversion of Control ,DI:Dependency Injection 依赖注入)和AOP(面向切面编程)功能,为应用系统提供声明式的安全访问控制功能,减少了为企业系统安全控制编写大量重复代码的工作。
ps:Spring security一定要基于spring
二、Spring security 引入?
(项目在IDEA中采用maven方式进行开发)
引入Spring Security很简单,只要在pom.xml文件中,引入spring security的依赖就可以了
ps:这里是新建了一个springboot工程,在springboot工程下引入
二、编写demo程序,体验spring security
1.首先附上官方开发文档
简单来说,demo程序需要一个创建两个class类,分别为WebSecurityConfig和TestController,WebSecurityConfig需要继承WebSecurityConfigurerAdapter,TestController在这里的作用相信不用多说各位都知道
- WebSecurityConfig具体:
- 通过 @EnableWebMvcSecurity 注解开启Spring Security的功能
- 继承 WebSecurityConfigurerAdapter ,并重写它的方法来设置一些web安全的细节
- configure(HttpSecurity http) 方法
- 通过 authorizeRequests() 定义哪些URL需要被保护、哪些不需要被保护。例如以上代码指定了 / 和 /home 不需要任何认就可以访问,其他的路径都必须通过身份验证。
- 通过 formLogin() 定义当需要用户登录时候,转到的登录页面。
- configureGlobal(AuthenticationManagerBuilder auth) 方法,在内存中创建了一个用户,该用户的名称为user,密码为user,用户角色为USER。
三、demo程序代码
WebSecurityConfig详细代码如下:
@Configuration
@EnableWebSecurity // 注解开启Spring Security的功能
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/", "/message/").permitAll() //定义不需要认证就可以访问
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login") //定义当需要用户登录时候,转到的登录页面
.permitAll()
.failureUrl("/login-error") //定义当登录失败的时候,转到的登录页面
.and()
.logout()
.permitAll();
http.csrf().disable();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder()).withUser("user")
.password(new BCryptPasswordEncoder().encode("user")).roles("USER");
//在内存中创建了一个用户,该用户的名称为user,密码为user,用户角色为USER
}
}
代码片段中configureGlobal使用了spring security官方推荐的bcrypt加密方式,若不加加密方式的话会有There is no PasswordEncoder mapped for the id “null” 的错误出现,网上百度了一下发现这是因为Spring security 5.0中新增了多种加密方式,也改变了密码的格式
TestController详细代码如下:
@Controller
public class TestController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/hello")
public String hello() {
return "hello";
}
@RequestMapping("/login")
public String login() {
return "login";
}
@RequestMapping("/login-error")
public String loginError(){
return "login-error";
}
}
项目目录结构如下:
其中hello.html,index.html,login.html,login-error.html为各个显示页面,比较简单也没用框架,代码就贴登录的那一部分吧
<!DOCTYPE html>
..
<title>Spring Security Example </title>
</head>
<body>
<form th:action="@{/login}" method="post">
<div><label> 用户名 : <input type="text" name="username"/> </label></div>
<div><label> 密 码 : <input type="password" name="password"/> </label></div>
<div><input type="submit" value="登录"/></div>
</form>
</body>
</html>
四、测试
浏览器输入http:localhost:8080会跳转至主页面index,如图
点击链接至hello.html,但由于我们没有设置hello.html可以任意访问,所以跳转到了login进行登录操作,成功才可以访问hello
输入错误账户密码效果如图
成功则进入hello界面
demo效果总体如上
五、总结
1.此项目搭建在springboot上,所以想试试的小伙伴又没接触过springboot的要去学习一下。
2.说到安全模块,比较流行的除了本文提到的Spring Security之外,还有Apache Shiro,两者之间的比较百度有很多博客详细说明,一个足够健壮,一个方便灵活,在此不多做描述。
3.实际开发肯定需要数据库等的加持,后续还会带来结合数据库等等版本。
六、代码下载
https://github.com/Rookie-xp/SpringSecurityDemo (可以的话给个star,十分感谢)
本人水平知识尚浅,大神勿喷。若有错误,欢迎指正。