在SpringSide中實現XFire Webservice認證,可以按照以下步驟進行:
<dependency>
<groupId>org.codehaus.xfire</groupId>
<artifactId>xfire-all</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
org.springframework.security.core.userdetails.UserDetailsService
接口的類,用于獲取用戶信息。可以根據實際業務需求自行實現。@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}
return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(),
Arrays.asList(new SimpleGrantedAuthority(user.getRole())));
}
}
org.springframework.ws.soap.security.xwss.XwsSecurityInterceptor
接口的類,用于配置Webservice的安全攔截器。@Component
public class XFireSecurityInterceptor extends XwsSecurityInterceptor {
@Autowired
private UserDetailsService userDetailsService;
@Override
public void afterPropertiesSet() throws Exception {
Wss4jSecurityInterceptor securityInterceptor = new Wss4jSecurityInterceptor();
securityInterceptor.setValidationActions("UsernameToken");
securityInterceptor.setSecurementActions("UsernameToken");
securityInterceptor.setSecurementUsernameTokenNoPassword(true);
securityInterceptor.setSecurementUsernameTokenDigestPassword(true);
securityInterceptor.setSecurementPasswordType(WSConstants.PASSWORD_DIGEST);
securityInterceptor.setValidationCallbackHandler(callbackHandler());
securityInterceptor.setValidationActions("UsernameToken");
securityInterceptor.setValidationSignatureCrypto(getCrypto());
securityInterceptor.setValidationDecryptionCrypto(getCrypto());
this.setInterceptors(new ClientInterceptor[]{securityInterceptor});
}
private CallbackHandler callbackHandler() {
return new PasswordCallbackHandler(userDetailsService);
}
private Crypto getCrypto() throws WSSecurityException {
Properties properties = new Properties();
properties.setProperty("org.apache.ws.security.crypto.provider",
"org.apache.ws.security.components.crypto.Merlin");
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", "keystorePassword");
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.alias", "alias");
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.file", "keystorePath");
Crypto crypto = CryptoFactory.getInstance(properties);
return crypto;
}
}
org.springframework.ws.soap.security.callback.CallbackHandler
接口的類,用于處理Webservice請求中的用戶名和密碼。public class PasswordCallbackHandler implements CallbackHandler {
private UserDetailsService userDetailsService;
public PasswordCallbackHandler(UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof UsernameCallback) {
UsernameCallback usernameCallback = (UsernameCallback) callback;
String username = usernameCallback.getUsername();
UserDetails userDetails = userDetailsService.loadUserByUsername(username);
if (userDetails == null) {
throw new IOException("User not found");
}
usernameCallback.setPassword(userDetails.getPassword());
} else {
throw new UnsupportedCallbackException(callback);
}
}
}
}
XFireSecurityInterceptor
和PayloadRootAnnotationMethodEndpointMapping
。<bean id="xfire" class="org.springframework.remoting.xfire.XFireFactoryBean">
<property name="inInterceptors">
<list>
<ref bean="xfireSecurityInterceptor"/>
</list>
</property>
</bean>
<bean id="xfireSecurityInterceptor" class="com.example.XFireSecurityInterceptor"
init-method="afterPropertiesSet">
<property name="userDetailsService" ref="userDetailsService"/>
</bean>
<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
<property name="interceptors" ref="xfire"/>
</bean>
以上