spring3 MVC及security在方法中获取用户名及角色以及提交form时字符串String与日期Date类型的绑定
spring3 MVC及spring security在方法中获取spring security用户名及角色以及提交form时字符串String与日期Date类型的绑定
1、在方法中获取spring security用户名及角色
private UserDetails getUserDetail(){
return (UserDetails) SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal(); www.zzzyk.com
}
private User getUser(){
String userName = getUserDetail().getUsername();
return service.findUserByLogName(userName);
}
private String getRoles(){
Collection<? extends GrantedAuthority> auths = getUserDetail().getAuthorities();
String result = "";
Object[] ats = auths.toArray();
for(int i=0; i<ats.length; i++){
GrantedAuthority o = (GrantedAuthority)ats[i];
result = result + o.getAuthority();
if(i != ats.length - 1){
result += ",";
}
}
return result;
}
2、提交form时字符串String与日期Date类型的绑定
@InitBinder
//此处的参数也可以是ServletRequestDataBinder类型
public void initBinder(WebDataBinder binder) throws Exception {
//注册自定义的属性编辑器
//1、日期
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
CustomDateEditor dateEditor = new CustomDateEditor(df, true);
//表示如果命令对象有Date类型的属性,将使用该属性编辑器进行类型转换
binder.registerCustomEditor(Date.class, dateEditor);
//自定义的电话号码编辑器(和【4.16.1、数据类型转换】一样)
//binder.registerCustomEditor(PhoneNumberModel.class, new PhoneNumberEditor());
}
补充:软件开发 , Java ,