Spring Boot 整合shiro模拟前后端分离
admin
2023-08-16 21:38:58
0次

Spring Boot 整合shiro模拟前后端分离 完整项目地址:https://github.com/Kahen/springboot-shiro2
加入全局异常监控

package com.example.aspect; import org.apache.shiro.authz.UnauthorizedException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import java.util.HashMap; import java.util.Map; /** * @author Kahen * @create 2020-02-01 11:13 */ @RestControllerAdvice //以json串的形式返回出去 public class AppExceptionAdivse { @ExceptionHandler(value= https://www.it610.com/article/{UnauthorizedException.class}) public Map, Object> unauthorized() { Map, Object> map=new HashMap<>(); map.put("code", 302); map.put("msg", "未授权"); System.out.println("未授权"); return map; }}

创建LoginController
package com.example.controller; import com.example.common.ActiverUser; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.subject.Subject; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpSession; import java.util.HashMap; import java.util.Map; /** * @author kahen */ @RestController @RequestMapping("login") public class LoginController { /** * 登陆 */ @RequestMapping("login") public Map,Object> login(String username, String password, HttpSession session) { Map,Object> map=new HashMap<>(); //封装token UsernamePasswordToken token=new UsernamePasswordToken(username, password); //得到主体 Subject subject = SecurityUtils.getSubject(); try { subject.login(token); ActiverUser activerUser = (ActiverUser) subject.getPrincipal(); session.setAttribute("user", activerUser.getUser()); map.put("code", 200); map.put("msg", "登陆成功"); return map; } catch (AuthenticationException e) { e.printStackTrace(); map.put("code", -1); map.put("msg", "登陆失败 用户名或密码不正确"); return map; } }}

创建UserController
package com.example.controller; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; /** * @author kahen */ @RestController @RequestMapping("user") public class UserController { @RequiresPermissions(value= https://www.it610.com/article/{"user:query"}) @RequestMapping("query") public Map,Object> query() { Map,Object> map=new HashMap<>(); map.put("msg", "query"); return map; } @RequiresPermissions(value= https://www.it610.com/article/{"user:add"}) @RequestMapping("add") public Map,Object> add() { Map,Object> map=new HashMap<>(); map.put("msg", "add"); return map; } @RequiresPermissions(value= https://www.it610.com/article/{"user:update"}) @RequestMapping("update") public Map,Object> update() { Map,Object> map=new HashMap<>(); map.put("msg", "update"); return map; } @RequiresPermissions(value= https://www.it610.com/article/{"user:delete"}) @RequestMapping("delete") public Map,Object> delete() { Map,Object> map=new HashMap<>(); map.put("msg", "delete"); return map; } @RequiresPermissions(value= https://www.it610.com/article/{"user:export"}) @RequestMapping("export") public Map,Object> export() { Map,Object> map=new HashMap<>(); map.put("msg", "export"); return map; } }

创建ShiroLoginFilter
package com.example.filter; /** * @author Kahen * @create 2020-02-01 11:28 */import com.alibaba.fastjson.JSONObject; import org.apache.shiro.web.filter.authc.FormAuthenticationFilter; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; public class ShiroLoginFilter extends FormAuthenticationFilter {@Override protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception { HttpServletResponse httpServletResponse = (HttpServletResponse) response; //if (isAjax(request)) { httpServletResponse.setCharacterEncoding("UTF-8"); httpServletResponse.setContentType("application/json"); Map, Object> resultData = https://www.it610.com/article/new HashMap<>(); resultData.put("code", -1); resultData.put("msg", "未登录!"); httpServletResponse.getWriter().write(JSONObject.toJSON(resultData).toString()); /* } else { // saveRequestAndRedirectToLogin(request, response); *//** * @Mark 非ajax请求重定向为登录页面 *//* httpServletResponse.sendRedirect("/login.jsp"); }*/ return false; }private boolean isAjax(ServletRequest request) { String header = ((HttpServletRequest) request).getHeader("X-Requested-With"); if ("XMLHttpRequest".equalsIgnoreCase(header)) { return Boolean.TRUE; } return Boolean.FALSE; } }

修改pom.xml引入fastjson
1.2.60 com.alibaba fastjson ${fastjson.version}

创建ShiroProperties 该项目有引入lombok插件
package com.example.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; /** * @author Kahen * @create 2020-01-19 20:47 */ @ConfigurationProperties(value = "https://www.it610.com/article/shiro") @Data public class ShiroProperties { private String hashAlgorithmName = "md5"; private Integer hashIterations = 2; private String loginUrl; private String unauthorizedUrl; private String[] anonUrls; private String logoutUrl; private String[] authUrls; }

创建ShiroAutoConfiguration
package com.example.config; import at.pollux.thymeleaf.shiro.dialect.ShiroDialect; import com.example.filter.ShiroLoginFilter; import com.example.realm.UserRealm; import org.apache.shiro.authc.credential.CredentialsMatcher; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.filter.DelegatingFilterProxy; import javax.servlet.Filter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author Kahen * @create 2020-01-19 20:45 */ @Configuration @EnableConfigurationProperties(ShiroProperties.class) public class ShiroAutoConfiguration {@Autowired private ShiroProperties shiroProperties; /** * 创建凭证匹配器 */ @Bean public HashedCredentialsMatcher credentialsMatcher() { HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher(); credentialsMatcher.setHashAlgorithmName(shiroProperties.getHashAlgorithmName()); credentialsMatcher.setHashIterations(shiroProperties.getHashIterations()); return credentialsMatcher; }/** * 创建realm */ @Bean public UserRealm userRealm(CredentialsMatcher credentialsMatcher) { UserRealm userRealm = new UserRealm(); //注入凭证匹配器 userRealm.setCredentialsMatcher(credentialsMatcher); return userRealm; }/** * 声明安全管理器 */ @Bean("securityManager") public SecurityManager securityManager(UserRealm userRealm) { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(userRealm); return securityManager; }/** * 配置过滤器 Shiro 的Web过滤器 id必须和web.xml里面的shiroFilter的 targetBeanName的值一样 */ @Bean("shiroFilter") public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) { ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean(); //注入安全管理器 bean.setSecurityManager(securityManager); //注入登陆页面 bean.setLoginUrl(shiroProperties.getLoginUrl()); //注入未授权的页面地址 bean.setUnauthorizedUrl(shiroProperties.getUnauthorizedUrl()); //注入过滤器 Map, String> filterChainDefinition = new HashMap<>(); //注入放行地址 if (shiroProperties.getAnonUrls() != null && shiroProperties.getAnonUrls().length > 0) { String[] anonUrls = shiroProperties.getAnonUrls(); for (String anonUrl : anonUrls) { filterChainDefinition.put(anonUrl, "anon"); } } //注入登出的地址 if (shiroProperties.getLogoutUrl() != null) { filterChainDefinition.put(shiroProperties.getLogoutUrl(), "logout"); } //注拦截的地址 String[] authcUrls = shiroProperties.getAuthUrls(); if (authcUrls != null && authcUrls.length > 0) { for (String authcUrl : authcUrls) { filterChainDefinition.put(authcUrl, "authc"); } } bean.setFilterChainDefinitionMap(filterChainDefinition); //创建自定义filter ShiroLoginFilter filter = new ShiroLoginFilter(); Map, Filter> map = new HashMap<>(); map.put("authc", filter); bean.setFilters(map); return bean; }/** * 注册过滤器 */ @Bean public FilterRegistrationBean filterRegistrationBeanDelegatingFilterProxy() { FilterRegistrationBean bean = new FilterRegistrationBean<>(); //创建过滤器 DelegatingFilterProxy proxy = new DelegatingFilterProxy(); bean.setFilter(proxy); bean.addInitParameter("targetFilterLifecycle", "true"); bean.addInitParameter("targetBeanName", "shiroFilter"); //bean.addUrlPatterns(); List> servletNames = new ArrayList<>(); servletNames.add(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); bean.setServletNames(servletNames); return bean; }/** * 这里是为了能在html页面引用shiro标签,上面两个函数必须添加,不然会报错 */ @Bean(name = "shiroDialect") public ShiroDialect shiroDialect() { return new ShiroDialect(); }/*加入注解的使用,不加入这个注解不生效--开始*//** * @param securityManager * @return */ @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) { AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; }@Bean public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() { DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); advisorAutoProxyCreator.setProxyTargetClass(true); return advisorAutoProxyCreator; } /*加入注解的使用,不加入这个注解不生效--结束*/ }

启动项目测试 项目启动后,在浏览器中输入
http://localhost:8080/login/login?username=zhangsan&password=123
会得到返回错误的json
【Spring Boot 整合shiro模拟前后端分离】Spring Boot 整合shiro模拟前后端分离
文章图片

访问查询用户
http://localhost:8080/user/query
Spring Boot 整合shiro模拟前后端分离
文章图片

访问正确的用户
http://localhost:8080/login/login?username=zhangsan&password=123456
Spring Boot 整合shiro模拟前后端分离
文章图片

重新访问http://localhost:8080/user/query
Spring Boot 整合shiro模拟前后端分离
文章图片

Spring Boot 整合shiro模拟前后端分离
文章图片

相关内容

热门资讯

大浪淘金APP黑幕揭秘:内盘操...   大浪淘金APP平台最隐蔽的黑幕,在于其完全封闭的内盘交易系统。这个看似与国际市场接轨的交易平台,...
告别固定工资实现财富自由?一品...   普通投资者被广告宣传诱导,注册就送现金,充值返现金券等等这些宣传方式,都是在引流到平台开户,不怕...
哈易购APP打着现货交易幌子搞...   网络上各类白银、铂金等贵金属投资广告铺天盖地,不少平台以"现货交易"为名,暗中却从事着非法期货交...
5元起投!高额收益?中国白银、...   上海华通白银国际交易中心有限公司(以下简称“华通白银国际”)打着“白银、铂金现货订购”的旗号,实...
"九州订购APP、起...   近年来,贵金属投资热度持续攀升,但乱象也随之而来。一些不法分子趁机搭建虚假交易平台,打着"低门槛...
坚锅淘金APP投资者频繁爆仓亏...   坚锅淘金APP对外宣称是贵金属现货交易平台,日常出入金看似畅通无阻,给人一种合规运营的假象。但事...
斗金订购APP“5元订购现货”...   斗金订购APP“5元订购现货”骗局揭秘:充值易、回本难,越陷越深!  许多投资人在未核实平台合规...
贵金属APP乱象曝光:&quo...   当前市场上部分贵金属类APP未取得正规交易资质,涉嫌误导消费者参与白银、铂金等商品的非合规交易!...
华通白银、银宝街APP被指虚假...   警惕现货白银投资陷阱:上海华通白银背后的平台涉嫌非法期货交易  近年来,各类现货白银交易平台以“...
充值送300,月入过万?别被量...   如今,网络上充斥着大量未经批准的贵金属及其他产品订购交易平台。这些平台普遍缺乏合法经营资质,却将...
鑫汇智投APP贵金属非法期货、...   鑫汇智投APP贵金属非法期货、人为控盘,大量投资者遭恶意收割  鑫汇智投APP长期以“低门槛、高...
矅光智策APP非法贵金属交易骗...   不少投资人在矅光智策APP上进行贵金属交易后出现了严重亏损。许多人在下载该平台时,根本不清楚其是...
飞鸽订购APP非法期货交易骗局...   飞鸽订购APP非法期货交易骗局,很多投资者根本没搞懂规则就盲目充值,买涨买跌跟买cai票一样!平...
天龙白银APP被操控的非法期货...   中小投资者在网络平台参与商品现货订购交易,表面看似合规合法,实则暗藏诸多难以察觉的风险。商品现货...
下载送试玩金?千万别碰!带你揭...   当前,许多贵金属APP交易平台正采用欺诈性营销手段。它们通过抖音等短视频平台,以“5元起投”“充...
泡泡淘金APP骗局:打着&qu...   泡泡淘金APP骗局:打着"商品订购"幌子,实则非法期货收割投资者!投资需谨慎,切勿轻信“高额回报...
南商云购APP虚假电子盘背后的...   南商云购是虚假电子盘吗?它是否具备合规的交易资质?这个曾经的"现货电子盘"如今摇身一变,披上了"...
警惕"祥龙订购&qu...   祥龙订购APP、掌上银牛APP等平台背后的运营方——四川禄宏商品现货市场有限公司,仅具备现货交易...
中国白银、华银:虚假宣传、后台...   中国白银APP宣称是上市公司“中国白银集团”与“上海华通白银国际交易中心”旗下的现货交易平台,提...
起底“优品订购”APP非法期货...   “优品订购”APP根本不具备任何合法的交易资质。其背后的运营方仅凭随便注册一家公司、开发一款交易...