我想使用angularjs發送用戶名和密碼登錄到彈簧安全性,而不是普通的login.jsp。我有一些配置如下圖所示:如何使用angularjs登錄彈簧安全
-security_config.xml
<http use-expressions="true" auto-config="true">
<access-denied-handler error-page="/403page" />
<anonymous enabled="false" />
<intercept-url pattern="/api/**" access="hasRole('ROLE_USER')" />
<custom-filter ref="resourceServerFilter" before="PRE_AUTH_FILTER" />
<access-denied-handler ref="oauthAccessDeniedHandler" />
<form-login login-page='/login' username-parameter="username"
password-parameter="password" default-target-url="/api/users"
authentication-failure-url="/login?authfailed" />
<logout logout-success-url="/login?logout" />
</http>
<authentication-manager alias="authenticationManager"
xmlns="http://www.springframework.org/schema/security">
<authentication-provider>
<password-encoder ref="encoder" />
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username,password, enabled from users where username=?"
authorities-by-username-query="select username, role from user_roles where username =? " />
</authentication-provider>
</authentication-manager>
<beans:bean id="encoder"
class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
<beans:constructor-arg name="strength" value="10" />
</beans:bean>
-LoginController.java
@RequestMapping("login")
public ModelAndView getLoginForm(@RequestParam(required = false) String authfailed, String logout,String denied) {
String message = "";
if (authfailed != null) {
message = "Invalid username of password, try again !";
} else if (logout != null) {
message = "Logged Out successfully, login again to continue !";
} else if (denied != null) {
message = "Access denied for this user !";
}
return new ModelAndView("loginForm", "message", message);
}
@RequestMapping("user")
public String geUserPage() {
return "user";
}
-loginForm.jsp
<form method="post" action="<c:url value='j_spring_security_check' />">
<table>
<tr>
<td colspan="2" style="color: red">${message}</td>
</tr>
<tr>
<td>User Name:</td>
<td><input type="text" name="username" />
</td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" />
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Login" />
</td>
</tr>
</table>
</form>
當我使用這樣的我可以正常登錄,但我不想使用上面的loginForm.jsp。我想通過angularjs使用restful web服務發送用戶名和密碼來爲安全性進行登錄。我能怎麼做?請幫幫我!
看看這個http://stackoverflow.com/questions/20538619/login-with -spring-security-using-ajax – kachhalimbu
我想使用angularjs比ajax –