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";
}
}

spring MVC,controller中获得resuqest和response的方式的更多相关文章

  1. Spring MVC Controller中解析GET方式的中文参数会乱码的问题(tomcat如何解码)

    Spring MVC Controller中解析GET方式的中文参数会乱码的问题 问题描述 在工作上使用突然出现从get获取中文参数乱码(新装机器,tomcat重新下载和配置),查了半天终于找到解决办 ...

  2. spring mvc controller中获取request head内容

    spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...

  3. Spring MVC Controller中GET方式传过来的中文参数会乱码的问题

    Spring MVC controller 这样写法通常意味着访问该请求,GET和POST请求都行,可是经常会遇到,如果碰到参数是中文的,post请求可以,get请求过来就是乱码.如果强行对参数进行了 ...

  4. spring MVC controller中的方法跳转到另外controller中的某个method的方法

    1. 需求背景     需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 本来以为挺简单的一 ...

  5. spring mvc Controller中使用@Value无法获取属性值

    在使用spring mvc时,实际上是两个spring容器: 1,dispatcher-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件 org.sp ...

  6. spring mvc controller中的异常封装

    http://abc08010051.iteye.com/blog/2031992 一直以来都在用spring mvc做mvc框架,我使用的不是基于注解的,还是使用的基于xml的,在controlle ...

  7. 在Spring MVC Controller中注入HttpServletRequest对象会不会造成线程安全的问题

    做法: 1.比如我们在Controller的方法中,通常是直接将HttpServletRequest做为参数,而为了方便节省代码,通常会定义为全局变量,然后使用@Autowire注入. 说明: 1.观 ...

  8. spring mvc controller中的参数验证机制(二)

    这里我们介绍以下自定义的校验器的简单的使用示例 一.包结构和主要文件 二.代码 1.自定义注解文件MyConstraint package com.knyel.validator; import ja ...

  9. spring mvc controller中的参数验证机制(一)

    一.验证用到的注解 @Valid 对传到后台的参数的验证 @BindingResult 配合@Valid使用,验证失败后的返回 二.示例 1.传统方式 @PostMapping public User ...

随机推荐

  1. OJ:神秘的数组初始化

    描述 填空,使得程序输出指定结果 #include <iostream> using namespace std; int main() { int * a[] = { // 在此处补充你 ...

  2. 走过路过不要错过 包你一文看懂支撑向量机SVM

    假设我们要判断一个人是否得癌症,比如下图:红色得癌症,蓝色不得. 看一下上图,要把红色的点和蓝色的点分开,可以画出无数条直线.上图里黄色的分割更好还是绿色的分割更好呢?直觉上一看,就是绿色的线更好.对 ...

  3. SELECT INTO和INSERT INTO SELECT的区别

    数据库中的数据复制备份 SELECT INTO: 形式: SELECT value1,value2,value3 INTO Table_2 FROM Table_1 Table_2表存在,报错:数据库 ...

  4. Java简单介绍及Java生态

    核心思想:面向对象编程,继承,高兼容(代码移植性强),开源,避免重复造轮子(使用mybatis,spring,redis等技术只需要将jar包依赖添加到项目中即可,jar包内就是技术核心代码,而这些框 ...

  5. VM扩展磁盘大小

    1.通过扩展磁盘的方法增大磁盘大小 2.然后开启Linux 此时查看磁盘  df -h 并没有增加,使用 fdisk -l 查看发现已经扩展 使用 root 用户,进入到 ~ 家目录下面. 3.使用 ...

  6. 异步是javascript的精髓

    最近做了一个智能家居的APP,目前纯JS代码已经4000多行,不包括任何引入的库.还在不断升级改造中...这个项目到处都是异步.大多数都是3-4层调用.给我的感觉就是异步当你习惯了,你会发现很爽.下面 ...

  7. 如何使用@vue/cli 3.0在npm上创建,发布和使用你自己的Vue.js组件库

    译者按: 你可能npm人家的包过成千上万次,但你是否有创建,发布和使用过自己的npm包? 原文: How to create, publish and use your own VueJS Compo ...

  8. mapper代理查询

    对于查询来说,要根据具体的业务,来指定mapper接口中方法的返回值类型1:如果只返回一条记录,mapper接口中方法的返回值类型应指定为pojo类型或其他简单类型,这样mybatis内部就会使用se ...

  9. JavaScript字符串转换为数字

    今天在工作中碰到了一个问题,要将字符串转换为数字,否则函数不能正常工作, 特地研究了下,写了2个函数,供大家参考,代码如下: /** * 将字符串转换为数字 * @param {Object} str ...

  10. PHP全路径无限分类导航LINK代码实现

    <?php /** * @param php全路径无限分类 */ include('db.inc.php'); function getPathCate($cateid){ $sql = &qu ...