如何关闭Spring Boot编码器

huangapple go评论61阅读模式
英文:

How to turn off Spring Boot Encoder

问题

以下是您提供的代码的翻译部分:

我需要在我的Spring Boot项目中关闭编码器这似乎很简单-只需删除方法和变量但然后我遇到了错误

> 不存在映射到IDnull的PasswordEncoder

现在我在数据库中有硬编码的用户登录名和已编码的密码密码存储为许多文字和数字在密码中它表示123”,我需要密码在数据库中为123”。以下是我的代码

    @Configuration
    @EnableWebSecurity
    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Autowired
        UserDetailsServiceImpl userDetailsService;
        @Bean
        public BCryptPasswordEncoder passwordEncoder() {
            return new BCryptPasswordEncoder();
        }
        @Autowired
        public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    
            auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
        }
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
    
    
            http.csrf().disable();
            http.authorizeRequests().
                    antMatchers("/", "/login", "/logout", "productPage", "contactsPage", "/register", "/add_person").permitAll();
            http.authorizeRequests().
                    antMatchers("/admin","/view_person")
                    .access("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER_ADMIN')");
            http.authorizeRequests().
                    and().exceptionHandling().accessDeniedPage("/403");
            http.authorizeRequests().and().formLogin()//
                    .loginProcessingUrl("/j_spring_security_check")
                    .loginPage("/login")//
                    .defaultSuccessUrl("/productPage")//
                    .failureUrl("/login?error=true")//
                    .usernameParameter("username")//
                    .passwordParameter("password")
                    .and().logout().logoutUrl("/logout").logoutSuccessUrl("/logoutSuccessful")
                     .and().rememberMe().key("secret").tokenValiditySeconds(500000);
    
        }
    }


    @Service
    public class UserDetailsServiceImpl implements UserDetailsService {
    
        @Autowired
        private AppUserDao appUserDAO;
    
        @Autowired
        private AppRoleDao appRoleDAO;
    
    
        @Override
        public UserDetails loadUserByUsername(String name)  {
            AppUser appUser = this.appUserDAO.findUserAccount(name);
    
            if (appUser == null) {
                System.out.println("用户未找到!" + name);
                throw new UsernameNotFoundException("用户 " + name + " 未在数据库中找到");
            }
    
            System.out.println("找到用户:" + appUser);
    
            List<String> roleNames = this.appRoleDAO.getRoleNames(appUser.getId());
    
            List<GrantedAuthority> grantList = new ArrayList<>();
            if (roleNames != null) {
                for (String role : roleNames) {
                    GrantedAuthority authority = new SimpleGrantedAuthority(role);
                    grantList.add(authority);
                }
            }
    
            UserDetails userDetails = new User(appUser.getName(),
                    appUser.getPassword(), grantList);
    
            return userDetails;
        }
    }

如果需要其他类的翻译,请随时提供。

英文:

I need to turn off encoder in my Spring boot project. It seems easy - just delete methods and variables, but then I get the error:

> There is no PasswordEncoder mapped for the id "null"

Now, i have hardcoded user login and (encoded) password in my database. Password is stored as many literals and numbers, and in password word it means "123", and i need password to be "123" in database. Here is my code:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserDetailsServiceImpl userDetailsService;
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().
antMatchers(&quot;/&quot;, &quot;/login&quot;, &quot;/logout&quot;, &quot;productPage&quot;, &quot;contactsPage&quot;, &quot;/register&quot;, &quot;/add_person&quot;).permitAll();
http.authorizeRequests().
antMatchers(&quot;/admin&quot;,&quot;/view_person&quot;)
.access(&quot;hasAnyRole(&#39;ROLE_ADMIN&#39;, &#39;ROLE_SUPER_ADMIN&#39;)&quot;);
http.authorizeRequests().
and().exceptionHandling().accessDeniedPage(&quot;/403&quot;);
http.authorizeRequests().and().formLogin()//
.loginProcessingUrl(&quot;/j_spring_security_check&quot;)
.loginPage(&quot;/login&quot;)//
.defaultSuccessUrl(&quot;/productPage&quot;)//
.failureUrl(&quot;/login?error=true&quot;)//
.usernameParameter(&quot;username&quot;)//
.passwordParameter(&quot;password&quot;)
.and().logout().logoutUrl(&quot;/logout&quot;).logoutSuccessUrl(&quot;/logoutSuccessful&quot;)
.and().rememberMe().key(&quot;secret&quot;).tokenValiditySeconds(500000);
}
}
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private AppUserDao appUserDAO;
@Autowired
private AppRoleDao appRoleDAO;
@Override
public UserDetails loadUserByUsername(String name)  {
AppUser appUser = this.appUserDAO.findUserAccount(name);
if (appUser == null) {
System.out.println(&quot;User not found! &quot; + name);
throw new UsernameNotFoundException(&quot;User &quot; + name + &quot; was not found in the database&quot;);
}
System.out.println(&quot;Found User: &quot; + appUser);
List&lt;String&gt; roleNames = this.appRoleDAO.getRoleNames(appUser.getId());
List&lt;GrantedAuthority&gt; grantList = new ArrayList&lt;&gt;();
if (roleNames != null) {
for (String role : roleNames) {
GrantedAuthority authority = new SimpleGrantedAuthority(role);
grantList.add(authority);
}
}
UserDetails userDetails = new User(appUser.getName(),
appUser.getPassword(), grantList);
return userDetails;
}
}

I can give any other classes if needed

答案1

得分: 0

代替您的BCryptPasswordEncoder,使用以下内容(尽管绝对不建议用于生产环境)。这将不会对您的密码输入进行哈希处理,这似乎是您想要的。

@Bean
public PasswordEncoder passwordEncoder() {
    return NoOpPasswordEncoder.getInstance();
}
英文:

Instead of your BCryptPassworencoder, use the following (though that's absolutely not recommended for production). This will not hash your password inputs, which is what you seem to want.

    @Bean
public PasswordEncoder passwordEncoder() {
return new NoOpPasswordEncoder.getInstance();
}

huangapple
  • 本文由 发表于 2020年4月10日 03:03:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/61128430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定