zoukankan      html  css  js  c++  java
  • spring MVC,controller中获得resuqest和response的方式

    package com.devjav.spring;
    
    import java.util.List;
    import java.util.Locale;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.security.core.GrantedAuthority;
    import org.springframework.security.core.context.SecurityContextImpl;
    import org.springframework.security.web.authentication.WebAuthenticationDetails;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    
    /**
     * Handles requests for the application home page.
     */
    @Controller
    public class HomeController {
    
        private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    
        /**
         * Simply selects the home view to render by returning its name.
         */
        @RequestMapping(value = "/home.do", method = RequestMethod.GET)
        public String home(HttpServletRequest request, HttpServletResponse response, Locale locale) {
            logger.info("Welcome User home! The client locale is {}.", locale);
    
            /*
             * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
             */
            SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession()
                    .getAttribute("SPRING_SECURITY_CONTEXT");
            // 登录名
            System.out.println("Username:" + securityContextImpl.getAuthentication().getName());
            // 登录密码,未加密的
            System.out.println("Credentials:" + securityContextImpl.getAuthentication().getCredentials());
            WebAuthenticationDetails details = (WebAuthenticationDetails) securityContextImpl.getAuthentication()
                    .getDetails();
            // 获得访问地址
            System.out.println("RemoteAddress" + details.getRemoteAddress());
            // 获得sessionid
            System.out.println("SessionId" + details.getSessionId());
            // 获得当前用户所拥有的权限
            List<GrantedAuthority> authorities = (List<GrantedAuthority>) securityContextImpl.getAuthentication()
                    .getAuthorities();
            for (GrantedAuthority grantedAuthority : authorities) {
                System.out.println("Authority" + grantedAuthority.getAuthority());
            }
            /*
             * ???????????????????????????????????????????????????????????????????
             */
    
            return "home";
        }
    
        @RequestMapping(value = "/admin/home.do", method = RequestMethod.GET)
        public String Adminhome(Locale locale) {
            logger.info("Welcome to Admin home! The client locale is {}.", locale);
    
            return "adminhome";
        }
    
        @RequestMapping(value = "/accessdenied.do", method = RequestMethod.GET)
        public String accessDenied() {
            logger.info("Access deniend.");
            return "accessdenied";
        }
    }
  • 相关阅读:
    1.ok6410移植bootloader,移植u-boot,学习u-boot命令
    ok6410按键中断编程,linux按键裸机
    四. jenkins部署springboot项目(1)--window环境
    一.jenkins安装(windows环境)
    oracle服务端导出/导入方式expdp/impdp
    linux 日志文件查看
    linux kafka进程挂了 自动重启
    kafka manager遇到的一些问题
    if条件语句
    shell脚本的条件测试与比较
  • 原文地址:https://www.cnblogs.com/bgo-tech/p/6586420.html
Copyright © 2011-2022 走看看