Witam, próbuje zrobić logowanie z bazą danych
Moje klasy
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@SuppressWarnings("deprecation")
@Bean
public static NoOpPasswordEncoder passwordEncoder() {
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}
@Bean
public UserDetailsService customUserDetailsService(){
return new CustomUserDetailsService();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("pass").roles("USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and()
.logout().and()
.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
.authorizeRequests().anyRequest().permitAll();
}
}
@Component
public class CustomUserDetailsService implements UserDetailsService {
private UserRepository userRepository;
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if(user == null){
throw new UsernameNotFoundException("User not found");
}
org.springframework.security.core.userdetails.User userDetails
= new org.springframework.security.core.userdetails.User(
user.getUsername(),
user.getPassword(),
convertAuthorities(user.getRole())
);
return userDetails;
}
private Set<GrantedAuthority> convertAuthorities(String roles){
Set<GrantedAuthority> authorities = new HashSet<>();
authorities.add(new SimpleGrantedAuthority(roles));
return authorities;
}
}
'use strict';
angular.module('app', ['ngRoute'])
.config(function ($routeProvider,$httpProvider) {
$routeProvider
.when('/login', {
templateUrl: 'partials/login.html',
controller: 'AuthenticationController',
controllerAs: 'authController'
})
$httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
})
.constant('LOGIN_ENDPOINT','/login')
.service('AuthenticationService', function($http, LOGIN_ENDPOINT) {
this.authenticate = function(credentials, successCallback) {
var authHeader = {Authorization: 'Basic ' + btoa(credentials.username+':'+credentials.password)};
var config = {headers: authHeader};
$http
.post(LOGIN_ENDPOINT, {}, config)
.then(function success(value) {
$http.defaults.headers.post.Authorization = authHeader.Authorization;
successCallback();
}, function error(reason) {
console.log('Login error');
console.log(reason);
});
}
this.logout = function(successCallback) {
delete $http.defaults.headers.post.Authorization;
successCallback();
}
})
.controller('AuthenticationController', function($rootScope, $location, AuthenticationService) {
var vm = this;
vm.credentials = {};
var loginSuccess = function() {
$rootScope.authenticated = true;
$location.path('/');
}
vm.login = function() {
AuthenticationService.authenticate(vm.credentials, loginSuccess);
}
});
<div >
<h1>Please Login</h1>
<form>
<input type="text" ng-model="authController.credentials.username"><br>
<input type="password" ng-model="authController.credentials.password"><br>
<button ng-click="authController.login()">Login</button>
</form>
</div>
Przy próbie zalogowania dostaje 404
A jak dodam taki kontroller ktory na spring.io ma mapping /user i ma zwracac 'currently authenticated user'
@RestController
public class AuthenticationController {
@RequestMapping("/login")
public Principal user(Principal user) {
return user;
}
}
To mogę się zalogować użytkownikiem którego mam inMemoryAuthentication() a jak chce się zalogować z bazy danych to dastaje 401