Spring3 MVC and security get the username and role in the method, and String and Date types when submitting form

Source: Internet
Author: User

Spring 3 MVC and spring security obtain the spring security username and role in the method, and bind the String to the Date type when submitting the form.

1. Get the spring security username and role in the Method

Private UserDetails getUserDetail (){
Return (UserDetails) SecurityContextHolder. getContext ()
. GetAuthentication ()
. GetPrincipal (); www.2cto.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. Bind String to Date type when submitting form

@ InitBinder
// The parameter can also be of the ServletRequestDataBinder type.
Public void initBinder (WebDataBinder binder) throws Exception {
// Register the custom property Editor
// 1. Date
DateFormat df = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss ");
CustomDateEditor dateEditor = new CustomDateEditor (df, true );
// Indicates that if the command object has a Date attribute, type conversion is performed using this Attribute Editor.
Binder. registerCustomEditor (Date. class, dateEditor );
// Custom phone number Editor (same as [4.16.1, data type conversion)
// Binder. registerCustomEditor (PhoneNumberModel. class, new PhoneNumberEditor ());
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.