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

SpringBoot-05-Security

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

SpringBoot-05-Security

一、环境搭建

①前端部分

所有页面代码类似




    
    Title


这是level1-1

 首页代码




    
    Title


    这是一个首页
    
        leve1
        1
        2
        3
    
    
        leve2
        1
        2
        3
    
    
        leve3
        1
        2
        3
    

    
        注销
        登录
    

路径控制

package com.gg.controller;

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

@Controller
public class RouterController {

    // 前往首页
    @RequestMapping({"/","/index"})
    public String index(){
        return "index";
    }

    // 前往登陆页面
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "views/login";
    }

    // 前往leve1
    @RequestMapping("/level1/{id}")
    public String level1(@PathVariable("id") int id){
        return "views/level1/"+id;
    }

    // 前往leve2
    @RequestMapping("/level2/{id}")
    public String level2(@PathVariable("id") int id){
        return "views/level2/"+id;
    }

    // 前往leve3
    @RequestMapping("/level3/{id}")
    public String level3(@PathVariable("id") int id){
        return "views/level3/"+id;
    }


}

②前端的页面访问

 

 二、授权和认证

重点:使用注解 

​
package com.gg.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 SecurityConfig extends WebSecurityConfigurerAdapter {

    //  链式编程   这个是基于HTTP请求的一同胡访问控制
    //  授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                //  首页所有人可以访问
                .antMatchers("/").permitAll()
                //  功能页面只有对应权限的人才能访问
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");
        //  没有权限会到登陆页面
        http.formLogin();
        //  开启注销功能
        http.logout();
    }

    //  认证
    //  密码编译:PasswordEncoder
    @Override 
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //  基于内存认证
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                //  对应的是用户:姓名   密码   角色    通过   and 拼接多个用户
                .withUser("lizhenguo").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2","vip3","vip1")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");

        // 使用密码   密码需要设置编码器   也就是加密
    }
}

​

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/749669.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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