当前位置:首页 » 《资源分享》 » 正文

安全框架Spring Boot 整合 Spring Security_DrLai的博客

2 人参与  2021年10月21日 12:43  分类 : 《资源分享》  评论

点击全文阅读


1.工业级安全框架介绍

        Spring Security基于Spring开发,项目中如果使用Spring作为基础,配合Spring Security做权限更加方便,而Shiro需要和Spring进行整合开发。因此作为spring全家桶中的Spring Security在java领域很常用。

2.建议搭建Spring Security环境

        2.1在pom.xml中添加相关依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springsecurityReview</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <artifactId>spring-boot-dependencies</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.5.4</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    </dependencies>
</project>

        2.2创建Handler类 

package com.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class Handler {
    @GetMapping("/index")
    public String index(){
        return "index";
    }
}

        2.3创建简单的html和配置相关thymeleaf的路径

         2.4最后再加个启动类,那么我们的整合测试就完成勒

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

        2.5成果展示 用户名默认user,密码则随机生成的这串数字

       

 

 3.进阶版使用

        3.1用户名和密码自定义

        

         3.2在config包下创建Encoder进行密码的校验和转码操作,将密码转成字符串形式,并通过match方法惊醒校验。 

        3.3赋予账号角色权限 

        

package com.example.config;

import org.springframework.context.annotation.Configuration;
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.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //角色和资源的关系
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/admin").hasRole("ADMIN")
        .antMatchers("/index").access("hasRole('ADMIN') or hasRole('USER') ")
                .anyRequest().authenticated()
        .and()
        .formLogin()
        .loginPage("/login")
        .permitAll()
        .and()
        .logout()
        .permitAll()
        .and()
        .csrf()
        .disable();

    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.inMemoryAuthentication().passwordEncoder(new MyPasswordEncoder())
       .withUser("user").password(new MyPasswordEncoder()
       .encode("000")).roles("USER")
       .and()
       .withUser("admin").password(new MyPasswordEncoder()
       .encode("123")).roles("ADMIN","USER");
    }
}

        最后达到admin账号能访问admin.html和index.html

        user只能访问index.html的操作


点击全文阅读


本文链接:http://www.zhangshiyu.com/post/30070.html

密码  创建  校验  
<< 上一篇 下一篇 >>

  • 评论(0)
  • 赞助本站

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新文章

  • 开挂辅助成团宠,全星际争当大冤种!结局+番外+完结白希薇沈星河完本_开挂辅助成团宠,全星际争当大冤种!结局+番外+完结(白希薇沈星河)
  • 多子多福?五旬老汉累断腰!大结局_张超柳三娘完整文本_小说后续在线阅读_无删减免费完结_
  • 好孕喜女重生嫁太子,把渣世子送进大内做太监高分神作_云州侯府子孙新书_小说后续在线阅读_无删减免费完结_
  • 我成为老赖后,请全公司狂欢的实习生慌了结局+番外榜单(陆暄江辞刘雯)全书免费_(陆暄江辞刘雯)我成为老赖后,请全公司狂欢的实习生慌了结局+番外榜单后续(陆暄江辞刘雯)
  • 等这最后七颗糖吃完,她就要彻底放下他了林舒寒江语静后续(林舒寒江语静)全篇在线阅读前传
  • 温愉贺聿风精选_温愉贺聿风精选
  • 温愉贺聿风+免费_温愉贺聿风+免费
  • 林清苒姜叙(她不想再继续这场四角恋)终章+番外(林清苒姜叙):结局+番外评价五颗星
  • 程允熙纪子尧小说(纪子尧程允熙)前文+全本完整阅读预售作品抢先看
  • 重生后换夫瘫痪私生子,失忆的京圈太子爷疯了小众冷门_许星宸宋知意妹妹爆火全网_小说后续在线阅读_无删减免费完结_
  • 小说林舒寒江语静(林舒寒江语静)已更新+无删减(等这最后七颗糖吃完,她就要彻底放下他了)
  • 方攸宁贺震霆(全家骗她吃苦,她偏要嫁豪门享福)(方攸宁贺震霆)完整章节列表_笔趣阁

    关于我们 | 我要投稿 | 免责申明

    Copyright © 2020-2022 ZhangShiYu.com Rights Reserved.豫ICP备2022013469号-1