shiro权限控制

1.shiro权限管理
1.新建shiroConfiguration类,与po、vo包同级

@Configuration
public class ShiroCinfiguration {

    //创建realm
    @Bean
    public NewsRealm getRealm(){return new NewsRealm();}
    
    //创建安全管理器
    @Bean
    public SecurityManager securityManager(NewsRealm realm){
        //使用默认的安全管理器
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(realm);
        //将自定义realm交给安全管理器统一调度管理
        return securityManager;
    }

    //配置shiroguol器工厂
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean();
        shiroFilterFactory.setSecurityManager(securityManager);
        //通用配置
        shiroFilterFactory.setLoginUrl("/admin");
        shiroFilterFactory.setUnauthorizedUrl("/admin");
        /*
        * key:请求路径
        * value:过滤器类型
        * */
        Map<String,String> filterMap = new LinkedHashMap<>();
        filterMap.put("/admin/types","perms[user-types]");
        filterMap.put("/admin/news","perms[user-news]");
        filterMap.put("/admin/tags","perms[user-tags]");
        filterMap.put("/admin/login","anon");
        filterMap.put("/admin/**","authc");
        System.out.println(filterMap);
//设置过滤器
        shiroFilterFactory.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactory;

    }
    //开启shiro注解支持
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }
}

2.建立role实体类

@Entity
@Table(name="t_role")
public class Role implements Serializable {
    private static final long serialVersionUID = 2057484709089375773L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String description;

    @ManyToMany(mappedBy = "roles")
    private Set<User> users = new HashSet<>(0);

    @ManyToMany(fetch = FetchType.EAGER)
    private Set<Permission> permissions = new HashSet<>(0);
}

3.建立permission实体类

@Entity
@Table(name="t_permission")
public class Permission implements Serializable{
    private static final long serialVersionUID = 423698596935410559L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String code;
    private String description;
}

5.logincontroller类修改

    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password,
                        HttpSession session, RedirectAttributes attributes){
        try{
            //构造登陆令牌
            UsernamePasswordToken upToken = new UsernamePasswordToken(username,password);
            //获取subject
            Subject subject = SecurityUtils.getSubject();
            subject.login(upToken);
            User user = (User) subject.getPrincipal();
            session.setAttribute("user",user);
            return "admin/index";
        }catch (Exception e){
            attributes.addFlashAttribute("message","用户名或密码错误");
            return "redirect:/admin";
        }
    }

2.springcloud eureka
在pom.xml文件中添加以下依赖

tk.mybatis
mapper-spring-boot-starter
2.0.4

5.po包中建立user实体类

@Table(name = “tb_user”)
public class User implements Serializable {
private static final long serialVersionUID = 532052702063464346L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String username;
private String password;
private String name;
private Integer age;
private Integer sex;
private Date birthday;
private Date created;
private Date updated;
}

7.mapper包中UserMapper类

@org.apache.ibatis.annotations.Mapper
public interface UserMappper extends Mapper {

}
8.Service包下建立UserService类

@Service
public class UserService {

    @Autowired(required = false)
    private UserMappper userMappper;

    public User queryById(Long id){
        return this.userMappper.selectByPrimaryKey(id);
    }
}

9在Controller包中新建UserController.类

@RestController
@RequestMapping("user")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @GetMapping("{id}")
    public User queryById(@PathVariable("id") Long id){
        return this.userService.queryById(id);
    }
}

10.对application.yml文件进行数据库连接配置:

server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db3?useSSL=true&characterEncoding=utf-8
    username: root
    password: 999
    driver-class-name: com.mysql.jdbc.Driver
  application:
    name: service-provider   #应用名称,注册到eureka后的服务名称
mybatis:
  type-aliases-package: com.lc.service.provider.po
eureka:
  client:
    service-url:  #EurekaServer地址
      defaultZone: http://localhost:10086/eureka
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值