spring security 和shiro简单使用

spring security

引入依赖

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

编写配置类

package com.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 {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //权限认证
        http.authorizeRequests().mvcMatchers("/").permitAll()//设置“/”路径全都可以访问
                .and().authorizeRequests().mvcMatchers("/level1/**").hasRole("vip1")//设置vip1的角色才可以访问"/level1/**"
                .and().authorizeRequests().mvcMatchers("/level2/**").hasRole("vip2")
                .and().authorizeRequests().mvcMatchers("/level3/**").hasRole("vip3");

        //没权限就返回登录界面  /login
        http.formLogin();

        //注销,注销成功后跳转到"/"
        http.logout().logoutSuccessUrl("/");

        //记住我功能
        http.rememberMe().rememberMeParameter("前端接收的名称");

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //授权
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("zhangsan").password(new BCryptPasswordEncoder().encode("123123")).roles("vip1")//设置哪个用户是哪个角色
                .and().withUser("lisi").password(new BCryptPasswordEncoder().encode("123123")).roles("vip2","vip3")
                .and().withUser("root").password(new BCryptPasswordEncoder().encode("root")).roles("vip1","vip2","vip3");
    }
}

shiro

引进依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.6.0</version>
        </dependency>

创建realm类对象,继承AuthorizingRealm

package com.config;


import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class userRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        System.out.println("执行了授权方法doGetAuthorizationInfo");
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行了验证方法doGetAuthenticationInfo");
        String username = "root";
        String password = "root";
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        if (!usernamePasswordToken.getUsername().equals(username)){
            return null;
        }
        //密码认证,shiro做
        return new SimpleAuthenticationInfo("",password,"");

    }
}

编写shiro配置类

package com.config;

import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

@Configuration
public class shiroConfig{

    //创建ShiroFilterFactoryBean对象
    @Bean
    public ShiroFilterFactoryBean definition(@Qualifier("SecurityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);
        Map<String, String> map = bean.getFilterChainDefinitionMap();
        /**
         * anon:无需认证就能访问
         * authc:需要认证才能访问
         * user:拥有 记住我 功能才能访问
         * perms:拥有对某个资源的权限才能访问
         * role:拥有某个角色权限才能访问
         */
        map.put("/add","authc");
        map.put("/del","authc");

        //没权限就返回登录界面
        bean.setLoginUrl("/toLogin");
        return bean;
    }

    //创建安全管理器对象
    //Qualifier表示使用哪个方法里的对象,也可取@Bean里的name属性值
    @Bean(name = "SecurityManager")
    public DefaultWebSecurityManager defaultSecurityManager(@Qualifier("userRealm") Realm realm){
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRealm(realm);
        return manager;
    }
    //创建realm对象
    @Bean
    public userRealm userRealm(){
        return new userRealm();
    }
}

在resources创建templates编写前端页面测试
在这里插入图片描述

编写controller

package com.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class shiroController {

    @RequestMapping({"/index","/"})
    public String index(){
        return "index";
    }

    @RequestMapping("/add")
    public String add(){
        return "user/add";
    }
    @RequestMapping("/del")
    public String del(){
        return "user/del";
    }
    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }
    @RequestMapping("/login")
    public String login(String username, String password, Model model){
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();

        //封装用户名和密码
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        try {
            subject.login(token);
            model.addAttribute("msg","登录成功!");
            return "index";
        }catch (UnknownAccountException e){
            model.addAttribute("msg","用户名错误!");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("msg","密码错误!");
            return "login";
        }

    }
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<p th:text="${msg}" style="color: red"></p>
<a th:href="@{/add}">add</a>
<a th:href="@{/del}">del</a>
</body>
</html>

login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p th:text="${msg}" style="color: red"></p>
<form th:action="@{/login}">
    <input type="text" name="username">
    <input type="text" name="password">
    <input type="submit" >
</form>
</body>
</html>

add.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>add</h1>

</body>
</html>

整合mybatis

引入依赖

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.49</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>

在realm对象userRealm里的认证方法里获取数据库查询的结果

@Autowired
UserService userService;
@Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("执行了验证方法doGetAuthenticationInfo");
//        String username = "root";
//        String password = "root";
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        User user = userService.geyAllUser(usernamePasswordToken.getUsername());//
        if (user.getName()==null){
            return null;
        }
        //密码认证,shiro做
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");

再对应添加service,mapper接口,serviceImpl实现类,user对象
ymal配置

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/study?characterEncoding=utf8
    username: root
    password: root
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  #config-location: classpath:mybatis/mybatis-config.xml  #全局配置文件位置
  mapper-locations: classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true #开启驼峰命名

在类内径下创建mapper包和对应的xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.UserMapper">

    <select id="gettUser" resultType="com.pojo.User">
        select * from user where name =#{name}
    </select>

</mapper>

启动测试

设置权限

在shiro配置类shiroConfig里设置进入某个路径需要有该路径的权限才能访问,否则跳到无权限界面

map.put("/add","perms[user:add]");
map.put("/del","perms[user:del]");

//没权限访问返回无权访问界面
bean.setUnauthorizedUrl("/available");

controller编写无权限路径

@RequestMapping("/available")
    @ResponseBody
    public String available(){
        return "无权访问!";
    }

可以在Realm对象里设置哪些路径都可以访问,当前用户只有什么权限

@Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        System.out.println("执行了授权方法doGetAuthorizationInfo");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addStringPermission("user:add");//给与这个路径授权
        //拿到当前的登录对象
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();

        //设置当前的用户权限
        info.addStringPermission(user.getPermission());
        return info;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring SecurityShiro都是Java领域中常用的安全框架,用于应用程序的身份验证和授权。它们提供了一些功能来保护应用程序的安全性,但在实现和使用上有一些区别。 1. Spring Security是一个基于Spring框架的安全解决方案,它提供了一套完整的认证和授权机制。它可以与Spring框架无缝集成,支持灵活的配置和扩展。Spring Security提供了许多内置的认证和授权特性,例如基于角色的访问控制、记住我功能、并发会话管理等。 2. Shiro是一个独立的安全框架,它可以与任何Java应用程序集成,不依赖于任何特定的框架。Shiro提供了简单易用的API,可以处理身份验证、授权、会话管理和密码加密等。与Spring Security不同,Shiro更加灵活,可以根据需求进行自定义配置。 在比较Spring SecurityShiro时,有几个方面需要考虑: 1. 集成和依赖:Spring SecuritySpring框架的一部分,与其他Spring组件集成非常方便。而Shiro是一个独立的框架,可以与任何Java应用程序集成。 2. 功能和扩展性:Spring Security提供了许多内置的认证和授权特性,并且具有强大的扩展性,可以满足大多数应用程序的需求。Shiro相对而言更加灵活,可以根据具体需求进行自定义配置。 3. 学习曲线和复杂性:Spring Security相对复杂一些,需要熟悉Spring框架的相关概念和配置。Shiro则相对简单,学习曲线较为平缓。 4. 社区支持和文档资料:Spring Security是一个非常流行的框架,有着庞大的社区支持和丰富的文档资料。Shiro虽然没有Spring Security那么大的社区,但也有一定的支持和文档资源可供参考。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值