0
我有一個應用程序使用spring security configure xml與spring MVC集成。如何將spring security xml轉換爲註釋
我的春節,安全配置XML:
<http pattern="/admin/**" authentication-manager-ref="adminAuthenticationManager">
<intercept-url pattern="/" access="permitAll" />
<form-login login-page="/login" username-parameter="userEmail"
login-processing-url="/admin/j_spring_security_check"
authentication-success-handler-ref="customAuthenticationSuccessHandler"
password-parameter="password" authentication-failure-url="/admin/login?error" />
<logout logout-url="/admin/j_spring_security_logout"
logout-success-url="/admin/login?logout" />
<remember-me key="uniqueAndSecret" user-service-ref="customUserDetailsService"
token-validity-seconds="7776000" />
<csrf disabled="true" />
</http>
<http authentication-manager-ref="userAuthenticationManager">
<intercept-url pattern="/" access="permitAll" />
<form-login login-page="/login" username-parameter="userEmail"
login-processing-url="/j_spring_security_check" password-parameter="password"
authentication-success-handler-ref="customAuthenticationSuccessHandler"
authentication-failure-url="/login?error" />
<logout logout-url="/j_spring_security_logout"
logout-success-url="/login?logout" />
<remember-me key="uniqueAndSecret" user-service-ref="customAdminDetailsService"
token-validity-seconds="7776000" />
<csrf disabled="true" />
</http>
<authentication-manager erase-credentials="false"
id="userAuthenticationManager">
<authentication-provider user-service-ref="customUserDetailsService" />
</authentication-manager>
<authentication-manager erase-credentials="false"
id="adminAuthenticationManager">
<authentication-provider user-service-ref="customAdminDetailsService" />
</authentication-manager>
<beans:bean id="customUserDetailsService"
class="com.service.impl.UserDetailServiceImpl" />
<beans:bean id="customAdminDetailsService"
class="com.service.impl.AdminDetailServiceImpl" />
我定製UserServiceDetailImpl文件:
@Service
public class UserDetailServiceImpl implements UserDetailsService {
@Autowired
private UserService userService;
@Override
public UserDetails loadUserByUsername(String email) {
try {
User user = userService.getUserByEmail(email);
if (user != null) {
return new org.springframework.security.core.userdetails.User(user.getUserEmail(), user.getPassword(),
true, true, true, true, getGrantedAuthorities(user));
} else {
throw new UsernameNotFoundException(email);
}
} catch (Exception e) {
throw new UsernameNotFoundException(email);
}
}
private List<GrantedAuthority> getGrantedAuthorities(Object generalUser) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
if (generalUser instanceof User) {
authorities.add(new SimpleGrantedAuthority(CommonConstant.Role.USER.toString()));
}
return authorities;
}
}
現在,我怎樣才能將其更改爲春天的註釋來配置?
我WebsercurityConfig文件時,我改變了春季安全使用註解:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailServiceImpl userDetailService;
@Autowired
AdminDetailServiceImpl adminDetailService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailService);
auth.userDetailsService(adminDetailService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/").permitAll()
.and().formLogin().loginPage("/login")
.failureUrl("/login?error").loginProcessingUrl("/j_spring_security_check")
.successHandler(loginSuccessHanlder).failureUrl("/login?error=true")
.usernameParameter("userEmail").passwordParameter("password")
.and().logout().logoutUrl("/j_spring_security_logout").logoutSuccessUrl("/login?logout")
.and().userDetailsService(userDetailService)
.rememberMe().key("uniqueAndSecret")
.tokenValiditySeconds(33343)
.and()
.authorizeRequests().antMatchers("/admin/**").permitAll()
.and().formLogin().loginPage("/login")
.failureUrl("/admin/login?error").loginProcessingUrl("/admin/j_spring_security_check")
.successHandler(loginSuccessHanlder).failureUrl("/admin/login?error=true")
.usernameParameter("userEmail").passwordParameter("password")
.and().logout().logoutUrl("/admin/j_spring_security_logout").logoutSuccessUrl("/admin/login?logout")
.and().userDetailsService(userDetailService)
.rememberMe().key("uniqueAndSecret")
.tokenValiditySeconds(33343);
}
}
感謝@kuhajeyan 你能告訴我如何自定義(或注入)豆以上 ? 在我的項目中,我定製UserDetailServiceImpl實現UserDetailService,但我不知道如何定製(或注入)上述 – joseJv
Bean應該只是簡單的自動裝配它,(更新) – kuhajeyan
嗨@kuhajeyan,你可以檢查我的WebSecurityConfig for我。我試圖追隨你的支持。但是我遇到了一些問題。 第一次,當我使用名稱空間「/ admin」登錄時,我的瀏覽器不處理任何事情,它只顯示「admin/j_spring_security_check」。 第二,當我用命名空間「/」登錄時,我調試程序,我用userEmail/password登錄true - >我的程序運行正確。但是當我用錯誤的userEmail/orPassword登錄時 - >我看到程序跳到UserDetailService 2次,然後跳轉到AdminDetailService。在任何時候,如果userEmail/password爲true,程序將中斷。 – joseJv