springmvc+shiro+angular实现前后端分离

k7060784 2017-02-10 03:55:41
自己做的小项目,登陆成功后获取菜单列表,均为ajax访问。登陆controller有设置subject.login,且在登陆的会话中是有认证的。但是当ajax访问获取菜单列表时,SecurityUtils.getSubject()获取的subject是未认证的,导致访问不到获取菜单列表的controller,并产生跨域问题。以下代码,麻烦大神看下!

shiro配置:
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<!-- 登录界面地址 -->
<property name="loginUrl" value="http://localhost:63342/warehouse/app/login.html"/>
<!-- 登录成功后地址 -->
<property name="successUrl" value="http://localhost:63342/warehouse/app/index.html"/>
<!-- 用户权限认证未通过时跳转到的url -->
<property name="unauthorizedUrl" value="http://localhost:63342/warehouse/app/error.html"/>
<!-- 登录权限校验 -->
<property name="filterChainDefinitions">
<value>
<!-- /static/login/** = anon
/static/js/myjs/** = authc
/static/js/** = anon
/uploadFiles/uploadImgs/** = anon
/code.do = anon -->
/user/webLogin.do = anon

<!-- /app**/** = anon
/weixin/** = anon -->
/** = authc
</value>
</property>
</bean>

登陆controller:

public Map<String, Object> webLogin(HttpServletRequest request,HttpServletResponse response,String loginName,String password,String randCode) throws Exception{
logger.debug("user login ");
response.setHeader("Access-Control-Allow-Origin", "*");
response.setContentType("text/json;charset=utf-8");

Map<String,Object> result = new HashMap<String, Object>();
//shiro管理的session
Subject subject = SecurityUtils.getSubject();
Session session = subject.getSession();
User user = new User();
user.setId("1");
user.setIp("1.1.1.1");
user.setLastLoginTime("2016.1.1");
user.setName("test1");
user.setPassword("11111");
user.setRights("all");
user.setRoleId("1");
user.setStatus("test");
user.setUsername("测试");
session.setAttribute("user", user);

//shiro加入身份验证
UsernamePasswordToken token = new UsernamePasswordToken(loginName, password);
token.setRememberMe(true);
try {
subject.login(token);
} catch (AuthenticationException e) {
System.out.println("身份验证失败!");
}
result.put("success", true);
result.put("msg", "登录成功");
return result;
}

获取菜单列表的controller:
public List<String> getAllmenuList(HttpServletResponse response){
response.setHeader("Access-Control-Allow-Origin", "*");
response.setContentType("text/json;charset=utf-8");
System.out.println("getAllmenuList start...");
List<String> menus = new ArrayList<String>();
menus.add("t1");
menus.add("t2");
menus.add("t3");
menus.add("t4");
menus.add("t5");
menus.add("t6");
return menus;
}

shiro自定义realm:
/*
* 登录信息和用户验证信息验证(non-Javadoc)
* @see org.apache.shiro.realm.AuthenticatingRealm#doGetAuthenticationInfo(org.apache.shiro.authc.AuthenticationToken)
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

String username = (String)token.getPrincipal(); //得到用户名
String password = new String((char[])token.getCredentials()); //得到密码
if(null != username && null != password){
return new SimpleAuthenticationInfo(username, password, getName());
}else{
return null;
}

}

/*
* 授权查询回调函数, 进行鉴权但缓存中无用户的授权信息时调用,负责在应用程序中决定用户的访问控制的方法(non-Javadoc)
* @see org.apache.shiro.realm.AuthorizingRealm#doGetAuthorizationInfo(org.apache.shiro.subject.PrincipalCollection)
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection pc) {

return null;
}
...全文
6585 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_39857878 2018-08-02
  • 打赏
  • 举报
回复
你在后台加上一个filter,允许跨域访问啊
YZCSDFYT 2018-04-25
  • 打赏
  • 举报
回复
ajax 默认是不同步session的,也就是说找不到会话的话就会每次请求都要重新认证,解决方法就只需要让异步请求带上浏览器的cookies就行,cookies里有sessionID会找回会话,修改ajax参数大概这样,建议自己封装好,整个项目就用自己的, xhrFields: { withCredentials: true }, crossDomain: true, 或者用$.fn.ajax.xhrFields来修改jquery框架的默认参数
  • 打赏
  • 举报
回复
您好,请问您解决了吗?
  • 打赏
  • 举报
回复
有人解决吗?求教
苍石 2017-10-10
  • 打赏
  • 举报
回复
引用 10 楼 matao_1125 的回复:
您好,请问您解决了吗?
http://blog.csdn.net/palerock/article/details/73457415 我自己总结的方法
苍石 2017-10-10
  • 打赏
  • 举报
回复
http://blog.csdn.net/palerock/article/details/73457415 我自己总结的方法
宿_ 2017-09-20
  • 打赏
  • 举报
回复
有人解决了吗?排队求解决防范
p531877327 2017-09-08
  • 打赏
  • 举报
回复
排队等解决 楼主问题解决了嘛
oyh1203 2017-07-19
  • 打赏
  • 举报
回复
你这个只是检验了用户是你定义的,但是没有给这个用户相应的设置权限呀,自定义的realm中的doGetAuthorizationInfo没有返回该有的权限信息
jink2005 2017-07-17
  • 打赏
  • 举报
回复
排队求大神解答。
什么都不能 2017-07-07
  • 打赏
  • 举报
回复
前后端分离,web 和 后台部署了不同的域名下产生了跨域的问题
fhliuzhihu 2017-07-07
  • 打赏
  • 举报
回复
同上,我也遇到过,也想问问,有没有哪位大佬解决的。
  • 打赏
  • 举报
回复
为什么会产生跨域问题?
qq_28290905 2017-07-04
  • 打赏
  • 举报
回复
我也遇到这样的问题 ,求解
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于SpringMVC+Spring+MyBatis (SSM) 架构的高效率便捷开发框架源码+项目说明.zip 本项目是一个整合 **SpringMVC+Spring+MyBatis(SSM)** 框架的 **Demo**。
拥有高效率便捷开发模式,使开发人员更专注于业务,达到面向业务开发。
项目使用 **Maven** 构建,便于项目管理,支持 **Oracle、MySql** 等主流数据库。
前端展示界面采用基于 **Boostrap** 实现的响应式布局,并集成了一系列的动画效果插件,整体界面简洁、美观大方并可优雅的与后台完成交互操作。
项目封装了一系列常用方法、部署运行简单,便于个人或企业进行高效开发。 ## 一、项目开发环境&工具(Environment&Tools) * MacOS Sierra / Windows 7 * MySql 5.7 * JDK 1.8 * CentOS 7 * IntelliJ IDEA 2017.2.5 / Eclipse 4.6.1 * Navicat Premium 11.1.12 * Maven 3.3.9 * Jetty 9.4.6.v20170531 / Tomcat 9.0.1 ## 二、技术选型(Technology) #### 1.服务端技术(Server) 名称 | 版本号 | 网址 --- | --- | --- Spring Framework | 4.3.13.RELEASE | [http://projects.spring.io/spring-framework/](http://projects.spring.io/spring-framework/) Shiro | 1.4.0 | [http://shiro.apache.org](http://shiro.apache.org) AspectJ | 1.8.13 | [http://www.eclipse.org/aspectj/](http://www.eclipse.org/aspectj/) MyBatis | 3.4.5 | [http://www.mybatis.org/mybatis-3/zh/index.html](http://www.mybatis.org/mybatis-3/zh/index.html) MyBatis Generator | 1.3.6 | [http://www.mybatis.org/generator/index.html](http://www.mybatis.org/generator/index.html) PageHelper | 5.1.2 | [http://git.oschina.net/free/Mybatis_PageHelper](http://git.oschina.net/free/Mybatis_PageHelper) Druid | 1.1.6 | [https://github.com/alibaba/druid](https://github.com/alibaba/druid) Jackson | 2.9.3 | [https://github.com/FasterXML/jackson](https://github.com/FasterXML/jackson) Dom4j | 1.6.1 | [http://www.dom4j.org](http://www.dom4j.org) Ehcache | 2.6.11| [http://www.ehcache.org/](http://www.ehcache.org/) Logback | 1.2.3 | [https://logback.qos.ch](https://logback.qos.ch) Maven | 3.3.9 | [http://maven.apache.org/](http://maven.apache.org/) #### 2.前端技术(Web) 名称 | 版本号 | 网址 --- | --- | --- angular | - | [https://angularjs.org](https://angularjs.org) awesome-bootstrap-checkbox | - | [https://github.com/flatlogic/aw

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧