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. React hooks实践

    前言 最近要对旧的项目进行重构,统一使用全新的react技术栈.同时,我们也决定尝试使用React hooks来进行开发,但是,由于React hooks崇尚的是使用(也只能使用)function c ...

  2. 安装haproxy和haproxy命令

    HaProxy系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.安装haproxy CentOS自带了haproxy,但可能版本比较老. ...

  3. 自定义了一个email模块,符合大多数人的使用习惯

    # coding: utf-8 import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text i ...

  4. python中的模块和包

    模块 一 什么是模块 模块就是一组功能的集合体,可以通过导入模块来复用模块的功能. 比如我在同一个文件夹定义两个.py文件,分别命名为A.py和B.py,那么可以通过在A文件里通过import B来使 ...

  5. Scala(一) —— 基础

    一.输出 println("Hello World") 二.变量与常量 1.变量用var表示,常量使用val表示 2.变量类型声明 var variableName : DateT ...

  6. [转]Docker版本变化和新版安装

    本文转自:http://www.cnblogs.com/Peter2014/p/7704306.html Docker从1.13版本之后采用时间线的方式作为版本号,分为社区版CE和企业版EE. 社区版 ...

  7. C#工具类:使用SharpZipLib进行压缩、解压文件

    SharpZipLib是一个开源的C#压缩解压库,应用非常广泛.就像用ADO.NET操作数据库要打开连接.执行命令.关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤.Sha ...

  8. MyBatis3-配置使用log4j输出日志

    配置步骤: 1.POM的依赖引入 <!-- log4j --> <!-- https://mvnrepository.com/artifact/log4j/log4j --> ...

  9. WPF 列表虚拟化时的滚动方式

    ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置.因此ListBox的默认模板中,含有ScrollVie ...

  10. Nmap 命令操作详解

    首先在安装nmap 稳定版 https://nmap.org/download.html 选择安装目录 通过cmd  去使用也可以在 安装目录中找到 进行可视化操作 以下是nmap 命令 -sT TC ...