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

Spring Security概述及初始配置

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

Spring Security概述及初始配置

Spring Security入门 一、概述

Spring Security基于Spring框架,提供了一套Web应用安全性的完整解决方案。一般来说,Web应用的安全性包括用户认证和用户授权两个部分,这两点也是Spring Security的重要核心功能。

(1)用户认证:验证某个用户是否为系统中的合法主体,也就是说用户能否访问改系统。用户认证一般要求用户提供用户名和密码。系统通过校验用户名和密码来完成认证过程。通俗点说就是系统认为用户是否能登陆。

(2)用户授权:要争某个用户是否有权限执行某个操作。在一个系统中,不同用户所具有的权限是不同的。比如对一个文件来说,育德用户只能进行读取,而有的用户可以进行修改。一般来说,系统会为不同的用户分配不同的角色,而每个角色则对应一系列的权限。通俗点将就是系统判断用户是否具有权限去做某些事情。

二、同款产品的对比

Spring Security的特点:

  1. 和spring无缝整合

  2. 全面的权限控制

  3. 专门为Web开发而设计

    • 就版本不能脱离Web环境使用
    • 新版本对整个框架进行了分层抽取,分成了核心模块和Web模块。单独引入核心模块就可以脱离Web环境。
  4. 重量级(需要依赖于很多其他的组件、引入各种其他的依赖)

Apache Shiro的特点:

  1. 轻量级:Shiro主张的理念是把复杂的事情变简单。针对对性能有更高要求的互联网应用有更好表现。
  2. 通用性:
    • 好处:不局限于Web环境,可以脱离Web环境使用
    • 缺陷:在Web环境下一些特定的需求需要手动编写代码定制

一般来说,常见的安全管理技术栈的组合是这样的:

  • SSM + Shiro
  • Spring Boot/Spring Cloud + Spring Security

以上只是一个推荐的组合而已,如果单纯从技术上来说,无论怎样组合都可以运行的。

三、web权限方案 1、设置登陆的用户名和密码

方法一:通过配置文件

在application.properties添加下列配置,账号密码换成自己定义的

spring.security.user.name=www
spring.security.user.password=wwwwww

方法二:通过配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    //重写设置账号密码的方法
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //密码加个密
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode("123");
        auth.inMemoryAuthentication().withUser("www").password(encode).roles("admin");
    }
    @Bean
    PasswordEncoder password(){
        return new BCryptPasswordEncoder();
    }
    }

以上两种方法都不灵活,通常账号和密码都是存在数据库里的,所以就有下列方法

方法三:自定义编写实现类

  1. 创建配置类,设置使用哪个userDetailsService实现

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
    import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    import org.springframework.security.core.userdetails.UserDetailsService;
    import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
    import org.springframework.security.crypto.password.PasswordEncoder;
    import javax.annotation.Resource;
    
    @Configuration
    public class SecurityConfigTest extends WebSecurityConfigurerAdapter {
        @Resource
        private UserDetailsService userDetailsService;
    
        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(userDetailsService).passwordEncoder(password());
        }
        @Bean
        PasswordEncoder password(){
            return new BCryptPasswordEncoder();
        }
        }
    
  2. 编写实现类,返回User对象,User对象有用户名密码和操作权限

    @Service("userDetailService")
    public class MyUserDetailsService implements UserDetailsService {
    
        @Resource
        private UsersMapper usersMapper;
    
        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            //调用usersMapper里面的方法查询数据库
            QueryWrapper wrapper = new QueryWrapper<>();
            wrapper.eq("username", username);
            Users users = usersMapper.selectOne(wrapper);
            if (users == null) {
                throw new RuntimeException("用户名不存在");
            }
            List auths = AuthorityUtils.commaSeparatedStringToAuthorityList("role");
            return new User(users.getUsername(), new BCryptPasswordEncoder().encode(users.getPassword() auths);
        }
    }
    
    @Data
    public class Users {
        private int id;
        private String username;
        private String password;
    }
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/643860.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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