我試圖使用spring安全性實現身份驗證。春季安全用戶身份驗證不起作用
我無法弄清楚我做錯了什麼。
web.xml中具有安全性濾波器:
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
彈簧security.xml文件具有定義的URL攔截並認證管理器:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-4.0.xsd">
<!-- enable use-expressions -->
<http auto-config="true" use-expressions="true">
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/logout**" access="permitAll" />
<!-- Incoming Product -->
<intercept-url pattern="/incomingProduct**" access="hasRole('Administrator') and hasRole('Local_Administrator') and hasRole('Supervisor') and hasRole('Manager')" />
<!-- Maintanence pages -->
<intercept-url pattern="/depotUser**" access="hasRole('Administrator') and hasRole('Local_Administrator')" />
<intercept-url pattern="/product**" access="hasRole('Administrator') and hasRole('Local_Administrator') and hasRole('Supervisor') and hasRole('Manager')" />
<intercept-url pattern="/productOwner**" access="hasRole('Administrator') and hasRole('Local_Administrator') and hasRole('Supervisor') and hasRole('Manager')" />
<intercept-url pattern="/storageTank**" access="hasRole('Administrator') and hasRole('Local_Administrator') and hasRole('Supervisor') and hasRole('Manager')" />
<intercept-url pattern="/admin**" access="hasRole('Administrator')" />
<!-- access denied page -->
<access-denied-handler error-page="/error/403" />
<form-login
login-page="/"
default-target-url="/"
authentication-failure-url="/Access_Denied"
username-parameter="username"
password-parameter="password" />
<logout logout-success-url="/logout" />
<!-- enable csrf protection -->
<csrf />
</http>
<authentication-manager>
<authentication-provider user-service-ref="userSecurityService" />
</authentication-manager>
<beans:bean id="userSecurityService" class="com.tms.securityServices.UserSecurityService" >
<beans:property name="depotUserDao" ref="depotUserDao" />
</beans:bean>
</beans:beans>
UserSecurityService實現的UserDetailsService。根據spring-security.xml中的配置,應該調用該命令來驗證登錄請求並將用戶注入會話。 (!請糾正我,如果我錯了)
@Transactional
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
{
DepotUser user = depotUserDao.findByUserName(username);
System.out.println("User : " + user);
if (user == null)
{
System.out.println("User not found");
throw new UsernameNotFoundException("Username not found");
}
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), user.isActive(), true, true, true,
getGrantedAuthorities(user));
}
private List<GrantedAuthority> getGrantedAuthorities(DepotUser user)
{
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
for (DepotUserRole userProfile : user.getUserRole())
{
System.out.println("UserProfile : " + userProfile);
authorities.add(new SimpleGrantedAuthority("ROLE_" + userProfile.getRole()));
}
System.out.print("authorities :" + authorities);
return authorities;
}
登錄控制器處理請求:
@RequestMapping(value = { "/loginRequest" }, method = RequestMethod.POST)
public String loginRequest(@RequestParam String username, @RequestParam String password, HttpServletRequest request, HttpServletResponse response)
{
DepotUser user = depotUserManager.getUserByUsernamePassword(username, password);
if (user != null)
{
request.setAttribute("firstName", user.getFirstName());
request.setAttribute("lastName", user.getLastName());
request.setAttribute("username", user.getUsername());
request.setAttribute("userRoles", user.getUserRole());
return "homePage";
}
當我登錄用戶獲取與匿名用戶登錄會發生什麼事。
身份驗證不會被觸發,因爲我沒有得到UserSecurityService中的中斷點。在處理請求的彈簧控制器中也是如此。
任何人都可以幫我嗎?
任何幫助表示讚賞。
謝謝,
由於@saljuama我試圖按照你建議,改變<表單登錄的登錄頁= 「/ loginRequest」。還是一樣的行爲。 –
控制器中的部件僅用於測試目的。 –
是的,測試可能會阻止春天的安全來做什麼:)。此外,@JacekWcislo在他的回答中說,你的登錄表單是怎樣的?你可以將代碼添加到你的問題嗎? – saljuama