2016년 8월 30일 화요일

Spring Security 인증방법 4가지.

JavaConfig 방식 기준으로 이야기를 하면 다음과 같다.
.xml 설정은 찾아보면 많이들 있더라.

1. in-memory

2. jdbc database

3. UserDetailsService

4. AuthenticationProvider

샘플 코드는 아래를 참조.
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
@Autowired
private AuthServiceImpl authService;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName();
String password = (String) authentication.getCredentials();
AdminUser user = (AdminUser) authService.loadUserByUsername(username);
if (user == null || !user.getUsername().equalsIgnoreCase(username)) {
throw new BadCredentialsException("Username not found.");
}
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
if (!passwordEncoder.matches(password, user.getPassword())) {
throw new BadCredentialsException("Wrong password.");
}
Collection<? extends GrantedAuthority> authorities = user.getAuthorities();
return new UsernamePasswordAuthenticationToken(user, password, authorities);
}
@Override
public boolean supports(Class<?> aClass) {
return true;
}
}
@Configuration
@EnableWebSecurity
public class WebMvcConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("userName")
.password("password")
.roles("ADMIN")
;
}
}
@Configuration
@EnableWebSecurity
public class WebMvcConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.usersByUsernameQuery(
"select username, password, enabled from admin_users where username=?")
.authoritiesByUsernameQuery(
"select username, role from user_roles where username=?")
;
}
}
@Configuration
@EnableWebSecurity
public class WebMvcConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(adminService)
;
}
}

2016년 8월 25일 목요일

kotlin을 포기하다

순수한 호기심으로 kotlin으로 앱을 만들어 보려고 했으나...

지극히 현실적인 문제로 포기하게 되었다.

androidStudio 버전업과 gradle2.14로 업데이트 하면서
빌드 에러가 속출하는 것이 아닌가.

매번 Clean & Run을 해야 하는데 kotlin 빌드가 그렇게 빠른것도 아니고
instant run도 포기해 가면서 이 짓을 해야 하는 생각이 많이 들었다.

비지니스 로직이나 모델 객체 등은 kotlin으로 만들어도 나쁘지 않은 것 같지만,
dagger2나 butterKnife등의 DI 라이브러리를 사용하기에 충돌도 많은 것 같고
어차피 안드로이드에 귀속되는 View나 API등은 굳이 kotlin으로 작성하지 않아도 될거 같다는 결론이다.