项目目录结构
和 E:\学习文档子目录压缩\框架\shiro\shiro安全框架入门\复习步骤7-获取权限数据CustomRealm提供subject桥梁,集成spring - 数据库获取用户权限角色等信息-shiro加密密码和盐存入数据库
不用的是:
pom.xml 中加了一个
<!-- spring切面 注解配置授权需要的dependency -->
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</dependency>
<!-- 注解配置授权需要的dependency -->
springmvc.xml
中加了
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"
<!--
proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。首先说明下proxy-target-class="true"和proxy-target-class="false"的区别,为true则是基于类的代理将起作用(需要cglib库),为false或者省略这个属性,则标准的JDK 基于接口的代理将起作用。
shiro 注解授权用
-->
<aop:config proxy-target-class="true" />
<bean class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"></property>
</bean>
<!-- shiro 注解授权用 -->
UserController.java
加了注解授权方法
package com.xiangshuai.controller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xiangshuai.vo.User;
/**
* maven 项目
* @author lqx
* 因为IDEA操作出现错误,故再次用eclispe进行操作,
* maven 项目- 文件位置:E:\学习文档子目录压缩\框架\shiro\shiro安全框架入门\复习步骤8-获取权限数据CustomRealm提供subject桥梁,集成spring -shiro注解配置权限
* 我的网盘/我的笔记/学习文档子目录压缩\框架\shiro\shiro安全框架入门\复习步骤8-获取权限数据CustomRealm提供subject桥梁,集成spring -shiro注解配置权限
* @create 2019-03-08 21:20
*/
@Controller
public class UserController {
//produces = "application/json; charset=utf-8" 解决@ResponseBody 返回中文乱码
@RequestMapping(value = "/subLogin",method = RequestMethod.POST
,produces = "application/json; charset=utf-8")
@ResponseBody
public String subLogin(User user){
Subject subject =SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
try {
subject.login(token);
if(subject.isAuthenticated()){//编程的方式 授权
return user.getUsername()+" 已经通过验证,可以登录";
}
if(subject.hasRole("admin")){//编程的方式 授权
return user.getUsername()+" 有 admin权限";
}
}catch (Exception e){
return e.getMessage();//将错误信息JSON响应到页面
}
return "可以登录";
}
/*通过注解配置授权*/
@RequiresAuthentication //shiro 必须是 通过认证的 才能进这个方法
@RequestMapping(value="/testAuthentication",method=RequestMethod.GET)
@ResponseBody
public String testAuthentication(){
return "testAuthentication success";
}
@RequiresRoles("admin") //shiro 必须是admin角色才能进这个方法-- 登录之后 请求 /testRole 就可以进来了
@RequestMapping(value="/testRole",method=RequestMethod.GET)
@ResponseBody
public String testRole(){
return "testRole success";
}
@RequiresPermissions({"user:add","user:update"}) //shiro 必须同时有 "user:add","user:update" 权限才能进的方法-- 登录之后 请求 /testQx 就可以进来了
@RequestMapping(value="/testQx",method=RequestMethod.GET)
@ResponseBody
public String testPermissions(){
return "testPermissions success";
}
/*通过注解配置授权*/
}