Spring Roo(2)build in STS and add security

本文介绍如何使用SpringRoo构建项目,并在Spring Tool Suite中导入该项目。此外,还详细说明了如何通过Selenium进行控制器层测试及配置Spring 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 4myhappylife@gmail.com --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("4myhappylife@gmail.com", "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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值