博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
atcrowdfunding-web
阅读量:6906 次
发布时间:2019-06-27

本文共 9151 字,大约阅读时间需要 30 分钟。

com.itliupengcheng.atcrowdfunding.controller

package com.atguigu.atcrowdfunding.controller;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import javax.servlet.http.HttpSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.atguigu.atcrowdfunding.bean.AJAXResult;import com.atguigu.atcrowdfunding.bean.Permission;import com.atguigu.atcrowdfunding.bean.User;import com.atguigu.atcrowdfunding.service.PermissionService;import com.atguigu.atcrowdfunding.service.UserService;@Controllerpublic class DispatcherController {    @Autowired    private UserService userService;    @Autowired    private PermissionService permissionService;        @RequestMapping("/login")    public String login() {        return "login";    }        @RequestMapping("/error")    public String error() {        return "error";    }        @RequestMapping("/logout")    public String logout(HttpSession session) {        //session.removeAttribute("loginUser");        session.invalidate();        return "redirect:login";    }        @RequestMapping("/main")    public String main() {        return "main";    }        @ResponseBody    @RequestMapping("/doAJAXLogin")    public Object doAJAXLogin(User user, HttpSession session) {                AJAXResult result = new AJAXResult();                User dbUser = userService.query4Login(user);        if ( dbUser != null ) {            session.setAttribute("loginUser", dbUser);                        //            List
permissions = permissionService.queryPermissionsByUser(dbUser); Map
permissionMap = new HashMap
(); Permission root = null; Set
uriSet = new HashSet
(); for ( Permission permission : permissions ) { permissionMap.put(permission.getId(), permission); if ( permission.getUrl() != null && !"".equals(permission.getUrl()) ) { uriSet.add(session.getServletContext().getContextPath() + permission.getUrl()); } } session.setAttribute("authUriSet", uriSet); for ( Permission permission : permissions ) { Permission child = permission; if ( child.getPid() == 0 ) { root = permission; } else { Permission parent = permissionMap.get(child.getPid()); parent.getChildren().add(child); } } session.setAttribute("rootPermission", root); result.setSuccess(true); } else { result.setSuccess(false); } return result; } /** * 鎵ц鐧婚檰 * @return */ @RequestMapping("/doLogin") public String doLogin( User user, Model model ) throws Exception { String loginacct = user.getLoginacct(); User dbUser = userService.query4Login(user); // 3) 鍒ゆ柇鐢ㄦ埛淇℃伅鏄惁瀛樺湪 if ( dbUser != null ) { // 鐧婚檰鎴愬姛锛岃烦杞埌涓婚〉闈� return "main"; } else { // 鐧婚檰澶辫触锛岃烦杞洖鍒扮櫥闄嗛〉闈紝鎻愮ず閿欒淇℃伅 String errorMsg = "鐧婚檰璐﹀彿鎴栧瘑鐮佷笉姝g‘锛岃閲嶆柊杈撳叆"; model.addAttribute("errorMsg", errorMsg); return "redirect:login"; } }}
DispatcherControlle.java

com.itliupengcheng.atcrowdfunding.web

package com.atguigu.atcrowdfunding.web;import java.util.HashSet;import java.util.List;import java.util.Set;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import com.atguigu.atcrowdfunding.bean.Permission;import com.atguigu.atcrowdfunding.service.PermissionService;public class AuthInterceptor extends HandlerInterceptorAdapter {    @Autowired    private PermissionService permissionService;        @Override    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)            throws Exception {        // 鑾峰彇鐢ㄦ埛鐨勮姹傚湴鍧�        String uri = request.getRequestURI();        String path = request.getSession().getServletContext().getContextPath();                // 鍒ゆ柇褰撳墠璺緞鏄惁闇�瑕佽繘琛屾潈闄愰獙璇併��        // 鏌ヨ鎵�鏈夐渶瑕侀獙璇佺殑璺緞闆嗗悎        List
permissions = permissionService.queryAll(); Set
uriSet = new HashSet
(); for ( Permission permission : permissions ) { if ( permission.getUrl() != null && !"".equals(permission.getUrl()) ) { uriSet.add(path + permission.getUrl()); } } if ( uriSet.contains(uri) ) { Set
authUriSet = (Set
)request.getSession().getAttribute("authUriSet"); if ( authUriSet.contains(uri) ) { return true; } else { response.sendRedirect(path + "/error"); return false; } } else { return true; } }}
AuthInterceptor.java
package com.atguigu.atcrowdfunding.web;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import com.atguigu.atcrowdfunding.bean.User;/** * 鐧婚檰鎷︽埅鍣� * @author 18801 * */public class LoginInterceptor implements HandlerInterceptor {    /**     * 鍦ㄦ帶鍒跺櫒鎵ц涔嬪墠瀹屾垚涓氬姟閫昏緫鎿嶄綔     * 鏂规硶鐨勮繑鍥炲�煎喅瀹氶�昏緫鏄惁缁х画鎵ц锛� true锛岃〃绀虹户缁墽琛岋紝 false, 琛ㄧず涓嶅啀缁х画鎵ц銆�     */    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)            throws Exception {                // 鍒ゆ柇褰撳墠鐢ㄦ埛鏄惁宸茬粡鐧婚檰        HttpSession session = request.getSession();        User loginUser = (User)session.getAttribute("loginUser");                if ( loginUser == null ) {            String path = session.getServletContext().getContextPath();            response.sendRedirect(path + "/login");            return false;            } else {            return true;        }    }    /**     * 鍦ㄦ帶鍒跺櫒鎵ц瀹屾瘯涔嬪悗鎵ц鐨勯�昏緫鎿嶄綔     */    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,            ModelAndView modelAndView) throws Exception {        // TODO Auto-generated method stub    }    /**     * 鍦ㄥ畬鎴愯鍥炬覆鏌撲箣鍚庯紝鎵ц姝ゆ柟娉曘��     */    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)            throws Exception {            }}
LoginInterceptor.java
package com.atguigu.atcrowdfunding.web;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class ServerStartupListener implements ServletContextListener {    public void contextInitialized(ServletContextEvent sce) {        // 灏唚eb搴旂敤鍚嶇О锛堣矾寰勶級淇濆瓨鍒癮pplication鑼冨洿涓�        ServletContext application = sce.getServletContext();        String path = application.getContextPath();        application.setAttribute("APP_PATH", path);    }    public void contextDestroyed(ServletContextEvent sce) {        // TODO Auto-generated method stub    }}
ServerStartupListener.java

config.xml

contextConfigLocation
classpath*:spring/spring-*.xml
org.springframework.web.context.ContextLoaderListener
com.atguigu.atcrowdfunding.web.ServerStartupListener
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encoding
springmvc
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring/springmvc-context.xml
1
springmvc
/
web.xml

 

转载于:https://www.cnblogs.com/liupengcheng0707/p/10964241.html

你可能感兴趣的文章
终极 Shell
查看>>
android使用系统资源,链接当前主题中的Style
查看>>
ajax笔记
查看>>
DPM2012学习(一),安装DPM2012
查看>>
objc_msg()报错
查看>>
巧用sytemd-journal清理日志
查看>>
设计模式--装饰者理解
查看>>
mysql 相关资料
查看>>
grep、egrep及相应的正则表达式和用法
查看>>
cisco Catalyst6500 6000和cisco7600如何破解密码
查看>>
docker下载镜像报net/http: TLS handshake timeout
查看>>
DOM-Node类型
查看>>
HttpClient是什么
查看>>
2018.3.27 二周第二次课
查看>>
文件迁移:将/home迁移到一个独立分区中
查看>>
网站seo如何利用指令查询网站收录
查看>>
Python语音识别终极指北,没错,就是指北!
查看>>
WPS文档转PDF格式的方法是什么
查看>>
大数据技术人员工具包最全集合
查看>>
Visual Paradigm 教程[UML]:从问题描述到模型
查看>>