基于狂神说springboot中的springSecrity

一:实现授权认证

自己先建一个springboot项目,这里就先不建了
1.导入静态资源模板
链接:https://pan.baidu.com/s/143hu-b_6dLKKqMOs05Ze8Q
提取码:iv3t
记得把view放到templates下面
2.在maven中引入thymeleaf模板和secrity的依赖包

 <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>`
        
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

3.建立一个controllerl类

@Controller
public class tsestController{
   @RequestMapping("/index")
   public String in(){
       return "index";

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

   }


   @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;
   }
}   

4.建立一个 Secrityconfig类继承自WebSecurityConfigurerAdapter

public class Secrityconfig extends WebSecurityConfigurerAdapter{

    /**
     * 授权
     * @param http
     * @throws Exception
     */
    @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()
   
    


    }

    /**
     * 认证
     * @param auth
     * @throws Exception
     */
    //在内存中定义,也可以在jdbc中去拿....
    //Spring security 5.0中新增了多种加密方式,也改变了密码的格式。
    //要想我们的项目还能够正常登陆,需要修改一下configure中的代码。我们要将前端传过来的密码进行某种方式加密
    //spring security 官方推荐的是使用bcrypt加密方式。


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("kuangshen").password( new BCryptPasswordEncoder().encode("111")).roles("vip1","vip2")
                .and()
                .withUser("zhang").password(new BCryptPasswordEncoder().encode("123456")).roles("vip3")
                .and()
                .withUser("chuanxin").password(new BCryptPasswordEncoder().encode("1")).roles("vip1","vip3");
    }
}
spring:
  thymeleaf:
    cache: false
server:
  port: 3333

访问地址是
在这里插入图片描述

二: 注销及权限控制

先在maven中导入thymeleaf和secrity的中间包

<dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
            <version>3.0.4.RELEASE</version>
</dependency>

在 Secrityconfig 中写上

  //开启注销
        http.logout();
        //注销成功返回首页
        http.logout().logoutSuccessUrl("/index");

在前端index页面加上
xmlns:sec=“http://www.thymeleaf.org/thymeleaf-extras-springsecurity5”
实现没登录显示登录,登录显示注销及其角色和用户名

 <!--登录注销-->
            <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">
                        <i class="address card icon"></i>
                        用户名:<span sec:authentication="principal.username"></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>
                                 

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值