Spring Roo(2)build in STS and add security

Spring Roo(2)build in STS and add security

1. rebuild the project
We create the projec with spring roo, so there is a file named log.roo. We can recreate a project with that file.
Delete other things and rebuild the project with commands. Create an empty directory conference, rename the file to conference.roo and place this file there.
>cd d:/work/roo/conference
>roo
>script --file conference.roo

I can import this projec into STS.

If I want to start the project in my STS. I need to click the project and -->[Properties]
----> [Deployment Assembly] -----> [Add]
[Java Build Path Entries] ----> [Next]
[Maven Dependencies] ----> [Finish]

After that, I can use tomcat, VMware VFabric tc Server Developer Edition v2.6 to deploy my app.

2. test with selenium
>roo
>selenium test --controller ~.web.SpeakerController
>selenium test --controller ~.web.TalkController

This will create the selenium test for controller layer.

Change src/main/webapp/selenium/test-speaker.xhtml
<tr>
<td>type</td>
       <td>_age_id</td>
       <td>25</td>
</tr>
to
<tr>
<td>type</td>
       <td>_age_id</td>
       <td>29</td>
</tr>

This will make some error in our test results.
>quit
>mvn jetty:run
>mvn selenium:selenese

It will stop when Launching Firefox. After I roll back my Firefox version to 3.5.1. It is ok now.

3. Expand the Spring Security
>roo
>security setup
Once this command is done, there will be a file applicationContext-security.xml.

Change this file from
<http auto-config="true" use-expressions="true">
        <form-login login-processing-url="/resources/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t" />
        <logout logout-url="/resources/j_spring_security_logout" />
        <!-- Configure these elements to secure URIs in your application -->
        <intercept-url pattern="/choices/**" access="hasRole('ROLE_ADMIN')" />
        <intercept-url pattern="/member/**" access="isAuthenticated()" />
        <intercept-url pattern="/resources/**" access="permitAll" />
        <intercept-url pattern="/**" access="permitAll" />
    </http>
to
<http auto-config="true" use-expressions="true">
        <form-login login-processing-url="/resources/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t" />
        <logout logout-url="/resources/j_spring_security_logout" />
        <!-- Configure these elements to secure URIs in your application -->
        <intercept-url pattern="/talks/**" access="hasRole('ROLE_ADMIN')" />
        <intercept-url pattern="/speakers/**" access="isAuthenticated()" />
        <intercept-url pattern="/resources/**" access="permitAll" />
        <intercept-url pattern="/**" access="permitAll" />
    </http>

That means only admin can use talks, and only authenticated users can enter speakers.

More details are learned before. So I do not practise so much here.

4. Email Notification
>email sender setup --hostServer smtp.gmail.com --username [email protected] --password password --port 587 --protocol SMTP

And I can change the email configuration in email.properties.

Add email template in talkController
>field email template --class ~.web.TalkController

This will add the mailTemplate and sendMessage method there.

refactor the method of create and encodeUrlPathSegment from
src/main/java/com/sillycat/roo/conference/web/TalkController_Roo_Controller.aj to TalkController.java as follow:

@RequestMapping(method = RequestMethod.POST, produces = "text/html")
public String create(@Valid Talk talk, BindingResult bindingResult, Model uiModel, HttpServletRequest httpServletRequest) {
if (bindingResult.hasErrors()) {
            populateEditForm(uiModel, talk);
            return "talks/create";
       }
       uiModel.asMap().clear();
       talk.persist();
       sendMessage("[email protected]", "Your talk is created", talk.getSpeaker().getEmail(), "Congrats your talk is created");
       return "redirect:/talks/" + encodeUrlPathSegment(talk.getId().toString(), httpServletRequest);
}

String encodeUrlPathSegment(String pathSegment, HttpServletRequest httpServletRequest) {
       String enc = httpServletRequest.getCharacterEncoding();
       if (enc == null) {
           enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
       }
       try {
           pathSegment = UriUtils.encodePathSegment(pathSegment, enc);
       } catch (UnsupportedEncodingException uee) {}
       return pathSegment;
}

It is done, once you create a talk, you can get one email.

references:
http://www.ibm.com/developerworks/cn/opensource/os-springroo2/index.html?ca=drs-
http://zk1878.iteye.com/blog/1222330


你可能感兴趣的:(spring roo)