栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Security

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Security

为什么使用SpringSecurity

在web开发中,安全占据第一位置。我们可以通过一些简单的安全策略,例如过滤器,拦截器保证安全。安全是一个非功能性需求,做网站,后台应该在设计之初进行考虑,在我们设计之前就应该把这些东西考虑进去,虽然我们可以通过拦截器,过滤器来完成需求,但是会有大量的原生代码,冗余,而通SpringSecurity,我们只需要进行简单的调用,便可实现无数复杂的功能项目中所涉及的安全性问题。
市面上比较知名的安全的框架

  • shiro
  • SpringSecurity
关于Spring Security

Spring Security是针对Spring项目的安全框架,也是Spring Boot底层安全模板默认的技术选型,他可以实现强大的Web安全控制,对于安全控制,我们仅需要引入spring-boot-starter-security模块,进行少量的配置,即可实现强大的安全管理

需要牢记的几个类

  • WebSecurityConfigurerAdapter: 自定义Security策略

  • AuthenticationManagerBuilder: 自定义认证策略

  • @EnableWebSecurity: 开启WebSecurity模式

“认证”(Authentication)

“授权”(Authorization)

这些概念是通用的,而不是只在Spring Security中存在
参考文档:
https://docs.spring.io/spring-security/site/docs/5.2.0.RELEASE/reference/htmlsingle

环境搭建
  1. 引入静态资源
  2. RouterControler跳往各个页面
package com.kuang.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RouterControler {

@RequestMapping({"/","/index"})
public String index(){
    return "index";
}

@RequestMapping("/toLogin")
public String toLogin(){
    return "views/login";
}

//跳往等级页面
@RequestMapping("/level1/{id}")
public String toLevel1(@PathVariable("id") int id){
    return "views/level1/"+id;
}

//跳往等级页面
@RequestMapping("/level2/{id}")
public String toLevel2(@PathVariable("id") int id){
    return "views/level2/"+id;
}

//跳往等级页面
@RequestMapping("/level3/{id}")
public String toLevel3(@PathVariable("id") int id){
    return "views/level3/"+id;
}
  1. 页面展示
导入Security

            org.springframework.boot
            spring-boot-starter-security

Security 配置类

继承WebSecurityConfigurerAdapter

package com.springboot_security.mao.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.password.Pbkdf2PasswordEncoder;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //链式编程
    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人可以访问,功能页只有对应有权限的人才能访问
        http.authorizeHttpRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
        //没有权限去等路面
        http.formLogin();
        //开启注销功能 ,注销成功回到首页
        http.logout().logoutSuccessUrl("/");
    }
    //认证
    //密码编码:PasswordEncode
    //在Spring Security 5.0+新增了很多加密方式
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //正常从数据库读
        auth.inMemoryAuthentication().passwordEncoder(new Pbkdf2PasswordEncoder())
                .withUser("mao").password(new Pbkdf2PasswordEncoder().encode("123456")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new Pbkdf2PasswordEncoder().encode("123456")).roles("vip1","vip2","vip3");
    }
}



错误403。权限错误

    //认证
    //密码编码:PasswordEncode
    //在Spring Security 5.0+新增了很多加密方式
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //正常从数据库读
        auth.inMemoryAuthentication().
                .withUser("mao").password("123456").roles("vip2","vip3")
                .and()
                .withUser("root").password("123456").roles("vip1","vip2","vip3");
    }

出现错误,密码未加密

thymeleaf与Spring Security整合

导入依赖


            org.thymeleaf.extras
            thymeleaf-extras-springsecurity4
            3.0.4.RELEASE
        

在index.html导入命名空间

 xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/841946.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号