Spring3.0 MVC preliminary 4-spring security REST

Source: Internet
Author: User

I. SpEl expressions

1. An AspectJ's pointcut expression language was used during transaction Configuration:

<Aop: config>
<Aop: advisor
Pointcut = "execution (** .. IUserService .*(..))"
Advice-ref = "txAdvice"/>
</Aop: config>

2. Use the SpEl expression to configure Security
<Intercept-url pattern = "/user/**" access = "hasRole ('Role _ admin')"/>
<Intercept-url pattern = "/**" access = "isAuthenticated ()"/>

Ii. view layer security elements
<% @ Taglib prefix = "security" uri = "http://www.springframework.org/security/tags" %>

Welcome: <security: authentication property = "principal. username"/>

Iii. web Request permission Control
<Intercept-url pattern = "/user/**" access = "hasRole ('Role _ admin')"/>
<Intercept-url pattern = "/role/**" access = "hasRole ('Role _ admin')"/>
<Intercept-url pattern = "/unit/**" access = "hasRole ('Role _ admin')"/>
<Intercept-url pattern = "/belong/**" access = "hasRole ('Role _ admin')"/>
<Intercept-url pattern = "/**" access = "isAuthenticated ()"/>


Iv. View-layer permission Control

<% @ Taglib prefix = "security" uri = "http://www.springframework.org/security/tags" %>

<Security: authorize access = "hasRole ('Role _ admin')">
<A href = "user/page/1"> user management </a> <br/>
<A href = "role"> role management </a> <br/>
<A href = "unit"> organization management </a> <br/>
<A href = "belong"> data attribution management </a>
</Security: authorize>

V. Method-layer permission Control

@ Secured ("ROLE_SPITTER ")
Public void addSpittle (Spittle spittle ){
//...
}

Vi. REST

1. Meaning: Representational State Transfer (REST)

Rest url: http: // t18: 3000/s4/user/4

Compare to struts2 url: http: // t18: 3000/s4/LoadUserAction. action? User. userId = 4

2. The controller can process all http requests, including GET, PUT, DELETE, and POST.

3. @ PathVariable annotation enables the Controller to process parameterized URLs

4. The spring label <sf: form method = "PUT"> works with the HeddenHttpMethodFilter filter to support the PUT and DELETE methods in a common browser.

Web. xml added

<Filter>
<Filter-name> httpMethodFilter </filter-name>
<Filter-class> org. springframework. web. filter. HiddenHttpMethodFilter </filter-class>
</Filter>
<Filter-mapping>
<Filter-name> httpMethodFilter </filter-name>
<Url-pattern>/* </url-pattern>
</Filter-mapping>


A. GET http: // t18: 3000/s4/user/4
@ RequestMapping (value = "{userId}", method = RequestMethod. GET)
Public String get (@ PathVariable ("userId") Short userId, Model model ){
User u = service. loadUser (userId );
Model. addAttribute (u );
Return "user/edit ";
}

B. display is used to modify PUT
<Sf: form method = "PUT" modelAttribute = "user">
Login Name <sf: input path = "logName"/> <br/>
Password <sf: input path = "password"/> <br/>
Real name <sf: input path = "userName"/> <br/>
Phone number <sf: input path = "phone"/> <br/>
Mobile phone <sf: input path = "mobilePhone"/> <br/>
Email <sf: input path = "email"/> <br/>
<Input type = "submit" value = "save"/>
</Sf: form>

C. Modify PUT

@ RequestMapping (value = "{userId}", method = RequestMethod. PUT)
Public String update (@ PathVariable Integer userId, @ Valid User user ){
Service. saveUser (user );
Return "redirect:/user/page/1 ";
}

D. DELETE

<Sf: form method = "DELETE" action = "user/$ {u. userId}">
<Input type = "submit" value = "delete"/>
</Sf: form>

@ RequestMapping (value = "{userId}", method = RequestMethod. DELETE)
Public String delete (@ PathVariable ("userId") short userId ){
User user = service. loadUser (userId );
Service. deleteUser (user );
Return "redirect:/user/page/1 ";
}
 

E. Prepare to add
 
@ RequestMapping (method = RequestMethod. GET, params = "new ")
Public String prepare (Model model ){
Model. addAttribute (new User ());
Return "user/edit ";
}

F. Use POST to submit the page. Controller:
@ RequestMapping (method = RequestMethod. POST)
Public String add (@ Valid User user, BindingResult result)
Throws BindException {
If (result. hasErrors ()){
Throw new BindException (result );
}
Service. addUser (user );
Return "redirect:/user/page/1 ";
}

VII. REST Paging

 

1. Paging
Public class Page implements IPageUtil {
Private int curPage = 1, toPage = 1, everyCount = 15;
Private long pageCount, count;
Boolean hasNext, hasPrevious;
Private List <?> Data = new ArrayList ();
 
Public Page (){
 
}
 
Public Page (int toPage, long count, int everyCount, List data ){
This. toPage = toPage;
This. count = count;
This. everyCount = everyCount;
This. curPage = getCurPage (count, everyCount );
This. data = data;
}

Public int getCurPage (long theCount ){
}
Public int getCurPage (long theCount, int n ){
}
...
}

2. dao support

Public Page findPagedListObject (String hql, int toPage, long count, int everyCount ){
Query query = getCurrentSession (). createQuery (hql );
If (toPage <= 0) toPage = 1;
Int first = (toPage-1) * everyCount;
Int max = everyCount;
Query. setFirstResult (first + 1 );
Query. setMaxResults (first + max );
List l = query. list ();
Page page = new Page (toPage, count, everyCount, l );
Return page;
}

3. service Support

Public Page listPagedUsers (int toPage, long count, int everyCount ){
Return dao. findPagedListObject ("from User u", toPage, count, everyCount );
}


4. Controller

@ RequestMapping (value = "/page/{toPage}", method = RequestMethod. GET)
Public String list (
@ PathVariable ("toPage") int toPage,
HttpServletRequest request,
Model model ){
Page page = service. findPagedUsers (request, toPage, service. countUser (request), 3 );
Model. addAttribute ("page", page );
 
Return "user/list ";
}
5. When data is displayed on the page, use $ {page. data }.

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.