SpringBoot-05-Security

一、环境搭建

①前端部分

所有页面代码类似

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1 style="text-align: center">这是level1-1</h1>
</body>
</html>

 首页代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    这是一个首页
    <div>
        <h1>leve1</h1>
        <a th:href="@{/level1/1}">1</a>
        <a th:href="@{/level1/2}">2</a>
        <a th:href="@{/level1/3}">3</a>
    </div>
    <div>
        <h1>leve2</h1>
        <a th:href="@{/level2/1}">1</a>
        <a th:href="@{/level2/2}">2</a>
        <a th:href="@{/level2/3}">3</a>
    </div>
    <div>
        <h1>leve3</h1>
        <a th:href="@{/level3/1}">1</a>
        <a th:href="@{/level3/2}">2</a>
        <a th:href="@{/level3/3}">3</a>
    </div>

    <div>
        <a th:href="@{/logout}">注销</a>
        <a th:href="@{/login}">登录</a>
    </div>
</body>
</html>

路径控制

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");

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

​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值