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";
}
}
spring MVC,controller中获得resuqest和response的方式的更多相关文章
- Spring MVC Controller中解析GET方式的中文参数会乱码的问题(tomcat如何解码)
Spring MVC Controller中解析GET方式的中文参数会乱码的问题 问题描述 在工作上使用突然出现从get获取中文参数乱码(新装机器,tomcat重新下载和配置),查了半天终于找到解决办 ...
- spring mvc controller中获取request head内容
spring mvc controller中获取request head内容: @RequestMapping("/{mlid}/{ptn}/{name}") public Str ...
- Spring MVC Controller中GET方式传过来的中文参数会乱码的问题
Spring MVC controller 这样写法通常意味着访问该请求,GET和POST请求都行,可是经常会遇到,如果碰到参数是中文的,post请求可以,get请求过来就是乱码.如果强行对参数进行了 ...
- spring MVC controller中的方法跳转到另外controller中的某个method的方法
1. 需求背景 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示. 本来以为挺简单的一 ...
- spring mvc Controller中使用@Value无法获取属性值
在使用spring mvc时,实际上是两个spring容器: 1,dispatcher-servlet.xml 是一个,我们的controller就在这里,所以这个里面也需要注入属性文件 org.sp ...
- spring mvc controller中的异常封装
http://abc08010051.iteye.com/blog/2031992 一直以来都在用spring mvc做mvc框架,我使用的不是基于注解的,还是使用的基于xml的,在controlle ...
- 在Spring MVC Controller中注入HttpServletRequest对象会不会造成线程安全的问题
做法: 1.比如我们在Controller的方法中,通常是直接将HttpServletRequest做为参数,而为了方便节省代码,通常会定义为全局变量,然后使用@Autowire注入. 说明: 1.观 ...
- spring mvc controller中的参数验证机制(二)
这里我们介绍以下自定义的校验器的简单的使用示例 一.包结构和主要文件 二.代码 1.自定义注解文件MyConstraint package com.knyel.validator; import ja ...
- spring mvc controller中的参数验证机制(一)
一.验证用到的注解 @Valid 对传到后台的参数的验证 @BindingResult 配合@Valid使用,验证失败后的返回 二.示例 1.传统方式 @PostMapping public User ...
随机推荐
- 监控报I/O问题,怎么办?
Linux系统出现了性能问题,一般我们可以通过top.iostat.free.vmstat等命令来查看初步定位问题.其中iostat可以给我们提供丰富的IO状态数据. 一.查询命令基本使用 1.命令介 ...
- spring boot多数据源配置(mysql,redis,mongodb)实战
使用Spring Boot Starter提升效率 虽然不同的starter实现起来各有差异,但是他们基本上都会使用到两个相同的内容:ConfigurationProperties和AutoConfi ...
- go跨平台编译
go语言支持直接编译不同系统的可执行程序,例如可以直接在mac上可以直接编译linux的执行程序 支持的环境变量 GOOS:目标可执行程序运行操作系统,支持 darwin,freebsd,linux, ...
- Apollo 7 — ConfigService 消息扫描设计实现
目录 设计 代码实现 总结 1.设计 Apollo 为了减少依赖,将本来 MQ 的职责转移到了 Mysql 中.具体表现为 Mysql 中的 ReleaseMessage 表. 具体官方文档可见:发送 ...
- 第一册:lesson sixty five.
原文: Not a baby. A:What are you going to do this evening Jill? B:I'm going to meet some friends, dad. ...
- 【转载】SQL语句用一个表的数据更新另一个表
在Sqlserver的维护更新操作中,有时候涉及到Update操作,其中有一种情况是根据特定的条件,以一个表中的数据更新另一个表的数据,此时涉及到两个表之间的关系以及操作,此处介绍2种更新方法. (1 ...
- cronolog日志切割catalina.out
cronolog日志切割catalina.out (一)解压安装cronolog 1:wget https://files.cnblogs.com/files/crazyzero/cronolog- ...
- [android] 绑定方式开启服务&调用服务的方法
需求:后台开启一个唱歌服务,这个服务里面有个方法切换歌曲 新建一个SingService继承系统Service 重写onCreate()和onDestory()方法 填一个自定义的方法changeSi ...
- php常用函数搜集
搜集了几个php常用函数方法....相信项目中肯定会用到吧... <?php /** * @param $arr * @param $key_name * @return array * 将数据 ...
- 5.QT-QString类
Qt中的字符串类 介绍 采用Unicode编码 采用隐式共享技术,节省内存和不必要的数据拷贝 隐式共享介于浅拷贝和深拷贝之间,当两个string对象赋值时,会实现浅拷贝(共享一块内存),如果某个对象 ...